如何在mogoose中嵌套填充中选择特定字段

时间:2016-06-01 07:28:26

标签: mongodb mongoose mongoose-populate mongoose-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
    },

});

用户链接平面模型 shortList.flat 平面模型与链接项目

现在我想尝试选择以下所有细节:

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

它的工作正常,但我想从项目模型中选择一些特定字段,如名称和图像

2 个答案:

答案 0 :(得分:21)

在填充中使用select属性:

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

这将只为您提供项目名称。

要指定您不希望的指定字段,您可以将其归零:

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

这适合我。

如果你正在做两个级别的人群:

我们可以简单地这样做:

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

用于获取特定字段的简单填充:

populate('project', 'name')

答案 1 :(得分:1)

尝试这样做:

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