Answer the question
In order to leave comments, you need to log in
How to get id in mongo?
Hello!
There is a collection, in it the document, the document respectively has a field _id.
Is it possible somehow in Mongo to read the document id from the database?
Now I'm doing this - I'm generating an id and inserting data into Mongo, then by the id that was generated earlier, I'm doing a data update.
Answer the question
In order to leave comments, you need to log in
So just using find we are looking for the _id field, any document always has it and is returned in any request. When writing, it is generated automatically. About working with find and _id is written in chapter 3 of the little mongo book
if you are looking for something like auto_increment in MySQL, then alas, no.
You will have to manually increase the field, for example using js functions in mongo.
What do you use to work with MongoDB? If you use Mongoose, then there is no such problem. We create a model, save it - you can easily get an id from the same model.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question