insert and return doc _id

9 views
Skip to first unread message

Toby Hede

unread,
Feb 5, 2012, 2:15:03 AM2/5/12
to clojure...@googlegroups.com
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?

Thanks,

Toby



Michael Klishin

unread,
Feb 5, 2012, 11:54:24 PM2/5/12
to clojure...@googlegroups.com
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.

MK

Toby Hede

unread,
Feb 11, 2012, 7:05:44 PM2/11/12
to clojure...@googlegroups.com
Thanks. 

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.
Reply all
Reply to author
Forward
0 new messages