user730569
user730569

Reputation: 4004

Manually generate id of document inserted into Mongodb

I want to get the id of a created document without having to make a separate query to get the document after it is created. I'm thinking I'll need to manually create the id of the document before creating it. Is there a way to do this? I'm using the mongoose driver.

Upvotes: 0

Views: 666

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311835

Once you create a new mongoose model instance, its _id value is already set to what it will be when saved. For example:

var user = new User();
// user._id is already set.
...
user.save();

Upvotes: 1

Related Questions