How to select specific field in nested populate in mogoose

前端 未结 2 1827
囚心锁ツ
囚心锁ツ 2021-02-04 06:02

here is my schema:

var UserSchema = new Schema({
    email: String,
    name: String,
    city: String,
    username: String,
    profilePic: String,
    phoneNo         


        
相关标签:
2条回答
  • 2021-02-04 06:36

    Use select property in populate as:

    User.populate(user, { path: 'shortList.flat.project', model: 'Project', select: 'name' })
    

    This will give you name of project only.

    To give specify fields which you don't want you can zero as:

    User.populate(user, { path: 'shortList.flat.project', model: 'Project', select: { 'name': 0, 'Floor': 0, 'flats': 0, 'towers': 0,} }
    

    This works for me.

    and if you are doing two level populations:

    we can simply do it as:

    populate('project.tower', 'name project flats');
    

    for a simple populate to get specific feilds:

    populate('project', 'name')
    
    0 讨论(0)
  • 2021-02-04 06:39

    try to do this:

    applicantListToExport: function (query, callback) {
      this
        .find(query).select({'advtId': 0})
        .populate({
          path: 'influId',
          model: 'influencer',
          select: { '_id': 1,'user':1},
          populate: {
           path: 'userid',
           model: 'User'
          }
        })
        .populate('campaignId',{'campaignTitle':1})
        .exec(callback);
      }
    
    0 讨论(0)
提交回复
热议问题