Is there an easy way to get the ID (ObjectID) of the last inserted document of a mongoDB instance using the Java driver?
+2
A:
Hate to answer my own question, but I just realized you can do this:
BasicDBObject doc = new BasicDBObject( "name", "Matt" );
collection.insert( doc );
ObjectId id = (ObjectId)doc.get( "_id" );
Matt W
2010-07-26 21:41:02
Valid question, don't forget to accept your own answer :)
Gates VP
2010-07-28 17:12:58
A:
I do not know about the Java driver but for posterity, the getLastError command can be run to get the _id of a write, even an upsert (as of 1.5.4)
chx
2010-07-30 06:02:50