Rhu*_*esh 9 mongoose mongodb 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
},
});
Run Code Online (Sandbox Code Playgroud)
这里用户链接与平面模型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);
})
});
Run Code Online (Sandbox Code Playgroud)
它的工作正常但我想从名称和图像中选择一些特定的字段来形成项目模型
Aru*_*agi 29
在填充中使用select属性:
User.populate(user, { path: 'shortList.flat.project', model: 'Project', select: 'name' })
Run Code Online (Sandbox Code Playgroud)
这将只为您提供项目名称.
要指定您不想要的指定字段,您可以将其归零:
User.populate(user, { path: 'shortList.flat.project', model: 'Project', select: { 'name': 0, 'Floor': 0, 'flats': 0, 'towers': 0,} }
Run Code Online (Sandbox Code Playgroud)
这适合我.
如果你正在做两个级别的人口:
我们可以简单地做到:
populate('project.tower', 'name project flats');
Run Code Online (Sandbox Code Playgroud)
对于一个简单的填充来获取特定的字段:
populate('project', 'name')
Run Code Online (Sandbox Code Playgroud)
尝试这样做:
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);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11318 次 |
最近记录: |