tags:

views:

12

answers:

1

Using the javascript api for MongoDB, how do you get the ID of the last inserted object? i.e.

obj = {a: 1, b: 2};
db.foo.insert(obj);

How do I get the ID for obj?

+1  A: 

The people on #mongodb were helpful. The JS API doesn't currently support this, but you can use your own id. To generate a "normal" id, do _id: new ObjectId(). So the example in my question would be:

id = new ObjectId();
obj = {a: 1, b: 2, _id: id};
db.foo.insert(obj);
Allen Rohner