Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select specific field in nested populate in mogoose

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

like image 597
Rhushikesh Avatar asked Jun 01 '16 07:06

Rhushikesh


2 Answers

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') 
like image 166
Arun Tyagi Avatar answered Sep 22 '22 00:09

Arun Tyagi


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);
  }
like image 32
Naveen Kumar Avatar answered Sep 25 '22 00:09

Naveen Kumar