Populate a mongoose model with a field that isn't an id

前端 未结 4 622
时光说笑
时光说笑 2020-12-02 19:10

Is it possible to populate a mongoose model with a field of a reference model that isn\'t the _id ... e.g. a username.

so something like

var personSc         


        
4条回答
  •  我在风中等你
    2020-12-02 19:36

    This is an example of using the $lookup aggregate to populate a model called Invite with the respective User based on the corresponding email field:

      Invite.aggregate(
          { $match: {interview: req.params.interview}},
          { $lookup: {from: 'users', localField: 'email', foreignField: 'email', as: 'user'} }
        ).exec( function (err, invites) {
          if (err) {
            next(err);
          }
    
          res.json(invites);
        }
      );
    

    It's probably quite similar to what you're trying to do.

提交回复
热议问题