Create and then populate? #1928

Closed
niftylettuce opened this Issue Feb 19, 2014 · 1 comment

Projects

None yet

3 participants

@niftylettuce
Contributor

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
  })
})
Contributor
TrejGun commented Dec 3, 2015

try this

model.findOneAndUpdate({_id:mongoose.Types.ObjectId()}, data, {
    new: true,
    upsert: true,
    runValidators: true,
    setDefaultsOnInsert: true,
    populate: options
})
@vkarpov15 vkarpov15 closed this Jun 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment