insert and return doc _id

Showing 1-3 of 3 messages
insert and return doc _id Toby Hede 2/4/12 11:15 PM
Really just to clarify my understanding

(collection/insert col doc) returns a WriteResult.

From my reading of the Java Driver API, the driver updates the doc parameter with the _id if one is generated as part of the insert. This doesn't happen in monger for obvious reasons. 

The conclusion seems to be that to insert a document and have access to the _id value, we should be creating the ObjectId explicitly and passing it in with the document. 

Is this actually, you know, like, correct?



Re: insert and return doc _id Michael Klishin 2/5/12 8:54 PM
This is the easiest way to do it. With persistent maps making it work the way Java driver works (which I personally found pretty confusing)
is tricky. You can take a look at one alternative demonstrated in this test:

I personally merge :_id (ObjectId.) explicitly for new documents and found it OK. This also works better
in tests where you sometimes want to load and validate inserted documented.

Re: insert and return doc _id Toby Hede 2/11/12 4:05 PM

The Java driver is definitely a bit strange, took me a while to grok the way it was working in Java.

Merging the _id seems to work fine, and makes the code less complicated and simple to test - keeping with that for now.