Retrieving DBObject from an array of DBObjects.

246 views
Skip to first unread message

Ravi Dontharaju

unread,
Mar 28, 2012, 3:07:53 PM3/28/12
to mongod...@googlegroups.com
Hi,

I am using Java API, and I have a collection which has an array of DBObjects under each DBObject document.

eg:

 { _id : "mainObject1"
   subObjects : [
   { _id : "subObject1", field : "value2" }, { _id : "subObject2", field : "value2" }
 ]}

P.S: example typed in email directly, so it is not a precise example.

Now, I have the _id value for a subObject, and I would like a way to retrieve the subObject directly into a DBObject, is there a way to do that?

Currently, what I do, is something like:

DBObject object = client.collection.findOne(new BasicDBObject("_id", "subObject1"));

And then I iterate thru the object.get("subObjects"); and try to match each _id again to get the DBObject that I need.

I would really like to know if there is an easier way!

Thanks,
Ravi

Marc

unread,
Mar 29, 2012, 5:46:40 PM3/29/12
to mongodb-user
You can modify embedded documents using dot notation and the "$"
operator. The documentation on dot notation is here:
http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29
and the documentation on the "$" operator is here:
http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator

However, to access individual embedded documents directly, the only
way is to retrieve the entire top-level document and iterate through
the array of embedded documents, as you have described.

On a related note: the new aggregation framework (released in 2.1.0)
has an $unwind method, which may be useful for your application: From
the documentation:
"$unwind peels off the elements of an array one by one, returning a
stream of documents. For each source document, for each member of the
specified array within it, a new document emerges, but with the
specified array replaced by one of its elements."

The aggregation framework documentation may be found here:
http://www.mongodb.org/display/DOCS/Aggregation+Framework
and the documentation specific to the $unwind method may be found
here:
http://www.mongodb.org/display/DOCS/Aggregation+Framework+-+%24unwind
It is not clear that the $unwind method is exactly what you are
looking for, but it may be worthwhile to experiment with.

I recently answered a question from another member of the community
regarding using the aggregation framework with the Java driver. If
you would like to try it out, the following Google Groups discussion
contains some relevant examples.
http://groups.google.com/group/mongodb-user/browse_thread/thread/b35d55f0a6997001

Hopefully, you will find the above links helpful.

Ravi Dontharaju

unread,
Mar 29, 2012, 6:07:36 PM3/29/12
to mongod...@googlegroups.com
I will experiment the unwind. I believe this is in the 2.0.5 Mongo
Release and 2.7.3 Java Driver?
This helps a lot! Thanks Marc!

- Ravi

> --
> 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.
>

Marc

unread,
Mar 30, 2012, 11:18:49 AM3/30/12
to mongodb-user
Excellent. I am happy to hear that you are trying out the aggregation
framework. However, this is only available in 2.1.0 (Development)
release. As an aside, Mongo 2.0.5 has not been released yet, so
unless you are sending this message from the Future, I believe that
this is incorrect. <grin>

Your current version of the Java driver (2.7.3) will be fine for using
the Aggregation Framework. Good luck! If you run into any questions,
the Mongo Community is here to help!

Marc

unread,
Mar 30, 2012, 11:39:18 AM3/30/12
to mongodb-user
Hello again. A colleague of mine brought to my attention a feature
request that would allow only one desired embedded document in an
array to be returned:
https://jira.mongodb.org/browse/SERVER-828 - Support for positional
($) operator in fields to return specifier This feature is slated for
the 2.1.1 release, although this is subject to change.

Additionally, while iterating through each embedded document is
functional for small arrays (only a handful of embedded documents), if
you anticipate that your embedded arrays will grow to any reasonably
large size, you may find that the overhead required to perform this
operation becomes prohibitive. If you find that your application is
often iterating through many embedded documents, you may want to
consider redesigning your schema such that the relevant documents are
on the top-level and may be queried directly.

Ravi Dontharaju

unread,
Mar 30, 2012, 1:16:54 PM3/30/12
to mongod...@googlegroups.com, mongodb-user
I guess I mashed up 1.8.5 and 2.0.4 and came up with 2.0.5! But you should know, I have a DeLorean DMC-12 hiding in my garage ready for use!! I am just waiting for the lighting.

My collection currently, maintains only a maximum of 5 DBObjects in my array per document. The Jira Server-828 would be exactly what I want.

I am trying out 2.1.0 development release with the latest Java driver. And as always, Mongo never fails to amaze me.

Also, I had a question regarding what protocol does Mongo use to discover and perform health check of other nodes in the set. Amazon Dynamo advised the use of gossip protocol. Is it the same for mongo? Is there a white paper that I can get for Mongo like there is one for Amazon Dynamo?

Thanks,
Ravi

Sent from my eMail ;)

Scott Hernandez

unread,
Mar 30, 2012, 1:23:51 PM3/30/12
to mongod...@googlegroups.com
On Fri, Mar 30, 2012 at 1:16 PM, Ravi Dontharaju
<ravi.do...@gmail.com> wrote:
> I guess I mashed up 1.8.5 and 2.0.4 and came up with 2.0.5! But you should know, I have a DeLorean DMC-12 hiding in my garage ready for use!! I am just waiting for the lighting.
>
> My collection currently, maintains only a maximum of 5 DBObjects in my array per document. The Jira Server-828 would be exactly what I want.
>
> I am trying out 2.1.0 development release with the latest Java driver. And as always, Mongo never fails to amaze me.
>
> Also, I had a question regarding what protocol does Mongo use to discover and perform health check of other nodes in the set. Amazon Dynamo advised the use of gossip protocol. Is it the same for mongo? Is there a white paper that I can get for Mongo like there is one for Amazon Dynamo?

It does this via a heartbeat command between each member via the mongo
wire protocol using custom commands.
http://www.mongodb.org/display/DOCS/Replica+Sets+-+Voting
http://www.mongodb.org/display/DOCS/Replica+Set+Commands
http://www.mongodb.org/display/DOCS/Mongo+Wire+Protocol

There is no white paper, but there is code :)

Reply all
Reply to author
Forward
0 new messages