How to select specific field in nested populate in mogoose

旧时模样 提交于 2020-01-23 04:55:29

问题


here is my schema:

var UserSchema = new Schema({
    email: String,
    name: String,
    city: String,
    username: String,
    profilePic: String,
    phoneNo: Number,
    shortList: {
        project: [{ type: Schema.ObjectId, ref: "Project" }],
        flat: [{ type: Schema.ObjectId, ref: "Flat" }],
    },

});

var FlatSchema = new Schema({
    project: { type: Schema.ObjectId, ref: "Project" },
    status: String,
    floor: Number,
    size: String,
    type: String,
    flat_number: String,
    price: Number,
    flooringType: String,
    createdAt: { type: Date, 'default': Date.now },
    isDeleted: { type: Boolean, 'default': false },
   });

var ProjectSchema = new Schema({
    name: String,
    shortName: String, 
    developer: { type: Schema.ObjectId, ref: "Developer" },
    address: {
        street_address: String,
        city: String,
        state: String,
        pin: Number,
        position: {
            lat: Number,
            long: Number
        }
    },
    ofcAddress: {
        street_address: String,
        city: String,
        state: String,
        pin: Number,
    },

    overview: {
        size: String,
        area: String,
        configuration: String,
        possession_starts: Date,
        info: String,
        aminities: [{ type: Schema.ObjectId, ref: "Amenity" }]
    },
    images: {
        hdpi: String,
        xhdpi: String,
        web: String,
        mdpi: String
    },

});

here user link with flat model shortList.flat and flat model is link with the project

now i am trying to select all details as follow:

User.findById(req.params.id)
        .populate('shortList.project', { 'CalculationsRules': 0, 'KBFlatTypeCharges': 0, 'CategoryPremia': 0, 'Floor': 0, 'KBUnitType': 0, 'floorplans': 0, 'flats': 0, 'towers': 0, 'galaryImages': 0 })
        .populate('shortList.flat', { 'pricedetails': 0 })
        .exec((err, user) => {
            if (err) { return handleError(res, err); }
            if (!user) { return res.json(401); }
//here i want to select specific field from project model
            User.populate(user, { path: 'shortList.flat.project', model: 'Project' },  (err, user) => {
                res.status(200).json(user);
            })
        });

its working fine but i want to select some specific fields form project model like name and images


回答1:


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')



回答2:


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);
  }


来源:https://stackoverflow.com/questions/37562129/how-to-select-specific-field-in-nested-populate-in-mogoose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!