question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Create and then populate?

See original GitHub issue

Is it possible to do something like this…?

db.model('User').create({ name: 'John' }).populate('_company').exec(function(err, user) {
    if (err) return next(err)
    console.log(user)
    // outputs user with populate user._company object from Company model
})

The only way I see it working now is…

db.model('User').create({ name: 'John' }, function(err, user) {
  if (err) return next(err)
  db.model('User').findById(user._id).populate('_company').exec(function(err, user) {
    if (err) return next(err)
    console.log(user)
    // outputs user with populate user._company object from Company model
  })
})

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
TrejGuncommented, Dec 3, 2015

try this

model.findOneAndUpdate({_id:mongoose.Types.ObjectId()}, data, {
    new: true,
    upsert: true,
    runValidators: true,
    setDefaultsOnInsert: true,
    populate: options
})
1reaction
mienaikoecommented, Apr 6, 2019

You can also do:

let user = await User.create({ ... })
user = await user.populate('company').execPopulate()
Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose populate after save - node.js - Stack Overflow
You should be able to use the Model's populate function to do this: http://mongoosejs.com/docs/api.html#model_Model.populate In the save handler for book, ...
Read more >
Mongoose v6.8.2: Query Population
In general, there is no way to make populate() filter stories based on properties of the story's author . For example, the below...
Read more >
Mongoose's Model.Populate() - Medium
And like magic, we have created a unified object using 2 schemas, 2 models, and 2 collections. All of the steps are important...
Read more >
Mongoose Populate() Method - GeeksforGeeks
In MongoDB, Population is the process of replacing the specified path in the document of one collection with the actual document from the ......
Read more >
How to use Populate in Mongoose & Node.js
If no document is found to populate, then field will be null . ... We are going to create 3 collections with 3...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found