I am using Java Driver for mongoDB. I have a document which contains, AlbumName, PreviewSize, ThumbSize, _id & Comments. PreviewSize & ThumbSize are array. I want to know how can i query this document and get any specific array value.
To be more clear, my document is like this,
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 shall i get the TomJerry2_468x342.jpg and its <Binary Data> from PreviewSize.
I am new to mongoDB. I dont know whether its possible or not. I just constructed this document and i am able to update this document with new images. I learnt this from this presentation.
My question how can i able to get any specific <Binary Data> when i have its name?
Any Suggestions would be more appreciative!
Thanks!!!
Update
For example: // The code i tried,
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>.*/
First i tried with by casting by (BasicDBObject), but i gave me a error like,
cannot cast (BasicBSONList) to (BasicDBObject) Then i changed it to (BasicBSONList)
I think i am doing wrong in the receiver. What receiver i should use!!!???
I tried the above one, and the error message i got in the console is,
Console Display:
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]"
I am totally new to mongoDB, like 3 days old :( .... Give me a way out!!!!
And, i also want to know is there any way to retrieve specific data,
Like, if i know the any ImageName can i get its BinaryData??
The query should be something like:
db.albums.find({ "ThumbSize.ImageName" : "TomJerry2_468x342.jpg"} )
That will return the Album document with all of the binary data.
You will not be able to return just that one piece. However, you can just return the "Thumbsize array" using the following.
db.albums.find({ "ThumbSize.ImageName" : "TomJerry2_468x342.jpg"}, { "ThumbSize" : 1 } )
However, if you have several large images, you'll want to look at GridFS for storing those images and then make a .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With