Sunday, July 15, 2012

Mongo DB: Java API Get Object by ID

You'll get an error if you try to do something along the lines of "{_id: xxxxx}".

This is how it's done:
BasicDBObject searchCriteria = new BasicDBObject("_id", new ObjectId(id));
DBObject dbObj = collection.findOne(searchCriteria);

No comments:

Post a Comment