How to select specific field in nested populate in mogoose

Rhushikesh picture Rhushikesh · Jun 1, 2016 · Viewed 15.9k times · Source

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

Answer

Arun Tyagi picture Arun Tyagi · Jun 1, 2016

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