question about BasicDBObject/DBObject and maps

633 views
Skip to first unread message

mwaschkowski

unread,
Apr 15, 2010, 6:14:48 PM4/15/10
to mongodb-user
Hi,

Mongo newb here with a question. I'm using the java driver and noted
(please correct if I'm wrong) that DBObject is just a Map that also
has the following:

markAsPartialObject
isPartialObject

Now, I'm aware that the Map is really a document that could be a tree
of embedded objects, which is great. My question is, are the above two
methods absolutely necessary, or could there be another (dumbed) class
that could be used as well? The reason I'm asking is because it would
simplify things for me if Mongo could just store maps. Currently I
have code (which is working but if something doesn't look right please
let me know) that converts a Map to a DBObject:

Map dbo = ...
DBObject queryObject = new BasicDBObject();
queryObject.putAll(dbo);

DBCollection collection = db.getCollection(collectionName);
DBObject dbObject = new BasicDBObject();
dbObject.putAll(dbo);
collection.insert(dbObject);
dbo.clear();
dbo.putAll(dbObject.toMap());
return dbo;


The front end I'm (GWT) would easily handle passing around Maps, but
if mongo could store maps too, I wouldn't need any translation code at
all...

Thanks,

Mark

Eliot Horowitz

unread,
Apr 15, 2010, 9:11:46 PM4/15/10
to mongod...@googlegroups.com
Yes - we need to not use Map directly so you can do do fast parsing
directly into classes, etc...

For your purposes though, you can cast BasicDBObject to Map since
unless you change something, a BasicDBObjcet will always be returned.
If you can't create a BasicDBobject to pass to mongo, you can easily
wrap by new BasicDBObject( Map m )
> --
> 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.
>
>

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

mwaschkowski

unread,
Apr 15, 2010, 9:44:37 PM4/15/10
to mongodb-user
> Yes - we need to not use Map directly so you can do do fast parsing
> directly into classes, etc...
Are you referring to:

http://www.mongodb.org/display/DOCS/Java+-+Saving+Objects+Using+DBObject
'collection.setObjectClass(Tweet);
Tweet myTweet = (Tweet)collection.findOne();'

If, so then I understand.

> For your purposes though, you can cast BasicDBObject to Map since
> unless you change something, a BasicDBObjcet will always be returned.
> If you can't create a BasicDBobject to pass to mongo, you can easily
> wrap by new BasicDBObject( Map m )
Yes, its not a lot of work.

Mongo has actually simplified a lot of my code I've noticed recently.
Its made a huge difference to my whole mindset when developing now,
its really a sea change kind of thing. I don't know what else to say
than you all really hit a high note with this.

Thank you.

Eliot Horowitz

unread,
Apr 15, 2010, 9:45:53 PM4/15/10
to mongod...@googlegroups.com
Yes - that's what I'm referring to.

mwaschkowski

unread,
Apr 16, 2010, 9:50:20 AM4/16/10
to mongodb-user
OK, thanks!
> > For more options, visit this group athttp://groups.google.com/group/mongodb-user?hl=en.
>
> --
> 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 athttp://groups.google.com/group/mongodb-user?hl=en.
Reply all
Reply to author
Forward
0 new messages