tags:

views:

138

answers:

2

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
Valid question, don't forget to accept your own answer :)
Gates VP
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