D
D
dvplut2014-10-27 12:48:50
MongoDB
dvplut, 2014-10-27 12:48:50

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

3 answer(s)
S
Sergey, 2014-10-27
@butteff

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

A
Alexander, 2014-10-27
@disc

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.

A
Alex Kheben, 2014-10-27
@zBit

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 question

Ask a Question

731 491 924 answers to any question