Querying array positions in MongoDB?

I am using the java driver for mongoDB. I have a document containing the AlbumName, PreviewSize, ThumbSize, _idand Comments. PreviewSizeand ThumbSize- arrays. I want to know how I can query this document and get the value of a specific array.

To be more clear, my document is as follows:

Document : { "AlbumName" : "Test" , "PreviewSize" : [ 
{ "ImageName" : "tom_and_jerry_guns.jpg" , "BinaryImage" : <Binary Data>} , 
{ "ImageName" : "Tom_and_Jerry,_Cartoons.jpg" , "BinaryImage" : <Binary Data>} , 
{ "ImageName" : "TomJerry2_468x342.jpg" , "BinaryImage" : <Binary Data>} , 
{ "ImageName" : "tom_and_jerry-5405.jpg" , "BinaryImage" : <Binary Data>}] , 

"ThumbSize" : [ { "ImageName" : "tom_and_jerry_guns.jpg" , "BinaryImage" : <Binary Data>} , 
{ "ImageName" : "Tom_and_Jerry,_Cartoons.jpg" , "BinaryImage" : <Binary Data>} , 
{ "ImageName" : "TomJerry2_468x342.jpg" , "BinaryImage" : <Binary Data>} , 
{ "ImageName" : "tom_and_jerry-5405.jpg" , "BinaryImage" : <Binary Data>}] , 

"_id" : { "$oid" : "4cef03b44613b5ba2d51a7b8"} , "comments" : [ ]}

How do I get TomJerry2_468x342.jpgit <Binary Data>from PreviewSize.

I am new to mongoDB. I do not know if this is possible or not. I just built this document and I can update this document with new images. I learned about this from this presentation.

My question is: how can I get any specific <Binary Data>when I have his name?

Any suggestions would be more valuable!

Thank!!!

://, ,

BasicDBObject query = new BasicDBObject(); 
BasicDBObject field = new BasicDBObject(); 
field.put("PreviewSize", 1); 
DBCursor cursor = coll.find(query, field); 
while(cursor.hasNext()){ 
     BasicDBObject result = (BasicDBObject) cursor.next(); 
     int i = result.size();
     System.out.println("Result Size: "+i);
     System.out.println(result);

     int i = result.size(); 
     String imageName = (String) ((BasicDBList)result.get("PreviewSize")).get("ImageName"); 
     System.out.println("Result Size: "+i+" Image Name :"+imageName); 
     byte[] imageByte = (byte[]) ((BasicDBList) result.get("PreviewSize")).get("BinaryImage"); 
     if(imageByte != null){System.out.println("Wow! Great!!! Its Coming!!!");} 
     else{System.out.println("Try Another Way!");} 
}// I am not sure this is exist(I am new to mongoDB). I need 
something like that. 
/* Some thing like the above list contains All contents of the 
"PreviewSize" data <ImageName> & <BinaryData>.*/ 

casting (BasicDBObject), , cannot cast (BasicBSONList) to (BasicDBObject) (BasicBSONList)

, . !!!???

, , ,

:

Result Size: 2
{ "PreviewSize" : [ { "ImageName" : "tom_and_jerry_guns.jpg" , "BinaryImage" : <Binary Data>} , 

{ "ImageName" : "Tom_and_Jerry,_Cartoons.jpg" , "BinaryImage" : <Binary Data>} , 

{ "ImageName" : "TomJerry2_468x342.jpg" , "BinaryImage" : <Binary Data>} , 

{ "ImageName" : "tom_and_jerry-5405.jpg" , "BinaryImage" : <Binary Data>}] , 

"_id" : { "$oid" : "4cef03b44613b5ba2d51a7b8"}}

"Exception in thread "main" java.lang.IllegalArgumentException: 
BasicBSONList can only work with numeric keys, not: [ImageName]" 

mongoDB, 3 :(.... !!!!

, , , - ImageName, BinaryData??

+3
1

:

db.albums.find({ "ThumbSize.ImageName" : "TomJerry2_468x342.jpg"} )

.

. Thumbsize, .

db.albums.find({ "ThumbSize.ImageName" : "TomJerry2_468x342.jpg"}, { "ThumbSize" : 1 } )

, , GridFS , .

+4

Source: https://habr.com/ru/post/1776990/


All Articles