Getting _id of inserted document

5 views
Skip to first unread message

mwa...@mz.com

unread,
Jun 13, 2017, 3:47:48 PM6/13/17
to luamongo
I'm enjoying using this library.

But I can't quite figure out how to get the '_id' of the document I just inserted.  E.g.

local db = assert(mongo.Connection.New())
assert(db:connect('localhost'))

local obj = {someKey = "someVal"}
local ans = db:insert('test.someDoc', obj)

print
(inspect(ans)) -- 'true'
print(inspect(obj)) -- my original object, unmodified

If I were to do this in Node.js, I could use the optional callback function parameter to 'insert'.  

Or, I could look at my inserted obj and expect to see  '__id' element tacked on.

But it doesn't appear either mechanism works.

Paco Zamora-Martínez

unread,
Jun 13, 2017, 3:59:27 PM6/13/17
to luamongo
Dear mwalsh,

Currently this feature is not implemented at luamongo. You should do a look up of your object to retrieve the Id.

Many thanks for your interest in luamongo. Regards

--
You received this message because you are subscribed to the Google Groups "luamongo" group.
To unsubscribe from this group and stop receiving emails from it, send an email to luamongo+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

mwa...@mz.com

unread,
Jun 13, 2017, 4:48:43 PM6/13/17
to luamongo
Sorry to be dense, but if I may follow-up, how would one look up the object if you don't know the _id? 

I could generate some kind of unique value and stick it in the document as part of the insert, then query on that, but then I have to make sure this value is truly unique.  Is some other way to look up the last inserted object?

Paco Zamora-Martínez

unread,
Jun 14, 2017, 2:16:19 AM6/14/17
to luamongo
Yes, I usually add a new index to the mongo table and use it for my convenience. Another way is to generate values for _id before the insert operation. However, in some scenarios, the _id is not relevant at all, so you only need to write down data and retrieve it depending on the values, not their identity.

Regards
Reply all
Reply to author
Forward
0 new messages