jeroen / mongolite

Fast and Simple MongoDB Client for R
https://jeroen.github.io/mongolite/
284 stars 64 forks source link

Need for insertOne() command in order to get the _id that was generated #175

Open LucasMelo3 opened 5 years ago

LucasMelo3 commented 5 years ago

There is a need to get the insertId field with the _id value of the just inserted document, which is already available in db.collection.insertOne() from the mongoDB.

There are database use cases that you need to retrieve the _id that was automatically generated, to use it right away after inserting. Is there any option available in mongolite? Is it possible to implement as insertOne?

Thank you in advance!

stennie commented 5 years ago

This is a duplicate of #80 (Get ID of inserted records), but a reasonable request.

Per the comment from ajdavis on the earlier issue, this is a current limitation of the underlying libmongoc driver API:

Hi, it's a bit inconvenient: you must generate the id's yourself when you create the documents. Or, if the documents are produced by user code, you must add the id's before calling mongoc_collection_insert, that way you know what the id's are and you can return them to the caller.

This is documented here:

http://mongoc.org/libmongoc/current/mongoc_collection_insert.html#description

I raised CDRIVER-3093: Include inserted _Ids in insert_one() and insert() results as an improvement for the C driver, which would be a dependency for this issue to be implemented.

Regards, Stennie

jrausch12 commented 1 year ago

Just ran into a big need for this myself with a production level system. Throwing my vocal support behind this.