如何select嵌套填充在猫鼬中的特定字段
How to select specific field in nested populate in mogoose
这是我的架构:
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
},
});
此处 用户 link 与 扁平 模型 shortList.flat flat 模型是 link 和 project
现在我正在尝试 select 所有详细信息如下:
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);
})
});
它工作正常,但我想 select 一些特定的字段形成项目模型,如名称和图像
使用 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')
尝试这样做:
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);
}
这是我的架构:
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
},
});
此处 用户 link 与 扁平 模型 shortList.flat flat 模型是 link 和 project
现在我正在尝试 select 所有详细信息如下:
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);
})
});
它工作正常,但我想 select 一些特定的字段形成项目模型,如名称和图像
使用 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')
尝试这样做:
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);
}