mongode icon indicating copy to clipboard operation
mongode copied to clipboard

Update the item in place with the newly created _id

Open jmzrlz opened this issue 15 years ago • 7 comments

Using mongodb-native, if you create an object then insert it into a collection, it is automatically updated in-place and '_id' is set.

So for example, if you begin with: x = { "a" : 5, "b" : 6} After inserting, it will be: x = {"_id" : someIDObject, "a": 5, "b" : 6}

However, when using mongode, this behaviour has been completely removed.

I'm requesting that you add it back as it is very useful when using the unique ID of each row after inserting it.

jmzrlz avatar Oct 20 '11 21:10 jmzrlz

Hi, can you post a gist of some sample code that I can use to recreate this issue? It seems to work fine for me using this test code: https://gist.github.com/1302796

vpulim avatar Oct 21 '11 00:10 vpulim

Simply try console.log(x) outside of the insert callback.

For example, using your code: https://gist.github.com/1304101

Of course, using callbacks is often a better way (e.g. so you can make use of the error var), but it isn't always convenient.

jmzrlz avatar Oct 21 '11 15:10 jmzrlz

On a side note, the bson_serializer class is also gone.

e.g. in native: x = new Db(...); x.bson_serializer.ObjectID(...)

in mongode: x = mongode.connect(...); console.log(x.bson_serializer); // undefined

jmzrlz avatar Oct 21 '11 15:10 jmzrlz

Ok, I see what you mean. So, doing this is in mongode is a little bit tricky since it buffers the collection connection operation. So when you call db.test.insert(x), you aren't guaranteed to have a valid collection yet. It will buffer that insert operation until you are able to connect to the collection. Therefore the insert() call won't happen until a short time after that line of code is executed if you aren't already connected.

I just pushed a new commit that lets you pass a callback to the db.collection() call that will be called when the collection is connected. You can move your insert code into this function if you need to generated ids immediately.

Example: https://gist.github.com/1304253

Alternatively, you can call db.collection() on startup and wait some time before you call insert().

Example: https://gist.github.com/1304251

Pull the latest commit if you want to test this out.

milewise avatar Oct 21 '11 16:10 milewise

Thanks a lot, I can manage using callbacks like you would normally for most of my code, but this does help for certain complicated parts.

Any idea if it'd be possible to get the bson_serializer working too? Not sure exactly how that class works initially so I'm unsure how you'd add it to mongode.

jmzrlz avatar Oct 21 '11 16:10 jmzrlz

bson_serializer and bson_deserializer are now available on the database object in mongode.

milewise avatar Oct 21 '11 16:10 milewise

Perfect, thanks for taking time to look into these issues.

jmzrlz avatar Oct 21 '11 17:10 jmzrlz