How to select specific field in nested populate in mogoose
Asked Answered
L

2

25

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

Lilly answered 1/6, 2016 at 7:28 Comment(0)
S
54

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')
Servetnick answered 1/6, 2016 at 7:46 Comment(2)
Thanks! this is what i need!Besmirch
Excellent solution. Works like a charm!Sandblind
G
10

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);
  }
Golda answered 14/6, 2017 at 19:20 Comment(1)
Best answer for meInescutcheon

© 2022 - 2024 — McMap. All rights reserved.