Seems like db.collection.insert() used to accept a callback function as last argument but the api has since changed and now it just returns a writeResult object reporting only the number of documents inserted..
How can I get the actual documents just inserted (or at least their _ids)?
Yes, I'm running my script in the mongo shell and I realize now the other stack overflow question I linked to above is about the node.js driver which has a different API (while it's also javascript and looks similar) and so:
mongo shell has never reported the details of inserted doc(s) and does not do so still (correct me if I'm wrong)The shell never returns the "_id" as such, but if you are using any scripting language this is how it can be done in python:
insert_result = db.collection.insert({"_id":10, "key":"value"})
The insert_result will contain the "_id" of the recently inserted document ie. 10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With