Accessing Array values in mongoDB with Java?

5,155 views
Skip to first unread message

MaRaVaN

unread,
Nov 25, 2010, 9:20:23 PM11/25/10
to mongodb-user
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 questions are how can i able to get the all the array contents at
once
and how can i able to get any specific <Binary Data> when i have its
name?

Any Suggestions would be more appreciative!

Thanks!!!

Eliot Horowitz

unread,
Nov 26, 2010, 12:04:41 AM11/26/10
to mongod...@googlegroups.com
Do you mean you want the server to only return a certain value?
If so you can use the $slice operator.

Otherwise not sure what you mean.

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

MaRaVaN

unread,
Nov 26, 2010, 1:10:36 AM11/26/10
to mongodb-user
Thanks for your response!!!

I just showed up a single document with 2 array lists.

Is there any way to get them all with one query?

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();
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>.*/

I tried the above one, and the error message i got in the comsole is,

"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 "ImageName" can i get its BinaryData??

MaRaVaN

unread,
Nov 26, 2010, 2:07:28 AM11/26/10
to mongodb-user
I tried to print the "result" Object and its size.

That is,

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"}}

You can compare it with the actual document posted in first mail.
Almost i can see what i wanted,
But i think i am doing wrong with its receiver. What type of receiver
should i use???!!!!

Thanks!

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

Eliot Horowitz

unread,
Nov 26, 2010, 9:23:50 AM11/26/10
to mongod...@googlegroups.com
You have to iterate the List and find the object you want.
If you want to access it via name, you can change the schema from use
an array to a sub-document.

MaRaVaN

unread,
Nov 27, 2010, 8:31:36 PM11/27/10
to mongodb-user
Thanks, that works...

But i am getting all images at once. Its not a problem when i need all
of them at once.

But, if need to show only one image means, i have to iterate through
all of the elements explicitly, like

if(cursor.hasNext())
{
BasicDBObject result = (BasicDBObject) cursor.next();
previewImageData = (BasicDBList) result.get("PreviewSize");
}
for(int i = 0; i<previewImageData.size(); i++)
{
String searchImage = (String) ((BSONObject)
previewImageData.get(i)).get("ImageName");
if(searchImage.equals(imageName))
{
get("ImageData";
}
}

After coding the above lines only i came to know about "$eleMatch". I
have also posted new post regarding "$eleMatch".

Don't think i am wasting your time by asking same thing in two
different posts. But both have totally different context!!
and i have got the above solution(inefficient) for this problem. But,
still i am in need of knowing "How to use "eleMatch" in java driver?


Thanks again!!
Reply all
Reply to author
Forward
0 new messages