Scala - Get Last Inserted ObjectId Using Casbah MongoDB

127 Views Asked by At

Is there any way to get last inserted ObjectId? i can't find it on their documentation or maybe i am not digging far enough, thanks.

1

There are 1 best solutions below

0
Ross On

The insert method will mutate the underlying DBObject and add a _id field if it doesn't exist.

> val document = MongoDBObject("{x: 101}")
> collection.insert(document)
> println(document)

{ "x" : 101 , "_id" : { "$oid" : "58204e47e9dd35767edb0225"}}

So you can check your inserted document for the _id