我有一个customer
架构,有两个子文档集; orders
和children
,如下所示:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const ObjectId = Schema.ObjectId;
const childrenSchema = new Schema({
'firstname': { 'type': String, 'required': true },
'lastname': { 'type': String, 'required': true },
'class': { 'type': String, 'required': true },
'school': { 'type': ObjectId, 'ref': 'Schools', 'required': true }
});
mongoose.model('Children', childrenSchema);
const ordersSchema = new Schema({
'date': { 'type': Date, 'required': true },
'delivery': { 'type': Date, 'required': true },
'item': { 'type': ObjectId, 'ref': 'Foods', 'required': true },
'child': { 'type': ObjectId, 'ref': 'Children', 'required': true }
});
mongoose.model('Orders', ordersSchema);
const customersSchema = new Schema({
'contact': {
'firstname': { 'type': String, 'required': true },
'lastname': { 'type': String, 'required': true },
'address': {
'road': { 'type': String, 'required': true },
'number': { 'type': String, 'required': true },
'floor': String,
'door': String,
'city': { 'type': String, 'required': true },
'zipcode': { 'type': String, 'required': true }
},
'email': { 'type': String, 'index': { 'unique': true }, 'required': true },
'phone': String
},
'password': { 'type': String, 'required': true },
'balance': { 'type': Number, 'default': 0.00 },
'active': { 'type': Boolean, 'default': false },
'children': [childrenSchema],
'orders': [ordersSchema]
});
mongoose.model('Customers', customersSchema);
现在,我的问题是,当我进行简单的查找和填充时,Customers.orders.child没有正确填充:
Customers.find()
.select('-password')
.populate('orders.item orders.child orders.child.school children.school')
.exec((err, result) => {
if (err) return console.log(err);
console.log(result);
});
示例结果:
{
"_id": "59cbb8335b5cdb08f85879b7",
"__v": 1,
"orders": [
{
"date": "2011-10-05T14:48:00.000Z",
"delivery": "2011-10-05T14:48:00.000Z",
"item": {
"_id": "59cbd9c78142782c30285bde",
"name": "Laksesandwich",
"description": "Saftig frisk laks i en hjemmebagt speltbolle med salat og dressing.",
"price": 38.5,
"__v": 0,
"image": "placeholder.png"
},
"child": null,
"_id": "59cbfe9f36b937296cbc76be"
}
],
"children": [
{
"firstname": "Ole",
"lastname": "Erling",
"class": "8A",
"school": {
"_id": "59cb876d7c700ec255eeb04d",
"name": "Københavns Skole",
"address": {
"road": "Københavnsvej",
"number": "133",
"city": "København",
"zipcode": "2000"
}
},
"_id": "59cbb8335b5cdb08f85879b8"
}
],
"active": false,
"balance": 0,
"contact": {
"firstname": "Albert",
"lastname": "Einstein",
"email": "genius@gmail.com",
"address": {
"road": "Hovedgaden",
"number": "42",
"city": "København",
"zipcode": "2000"
}
}
}
orders.child是null
,但应该是给出_id的子文件的填充文档。
我该如何管理?
答案 0 :(得分:0)
试试这个:
Customers.find()
.select('-password')
.populate('orders.item children.school')
.populate({
path: 'orders.child',
model: 'Children',
populate: {
path: 'orders.child.school',
model: 'Schools'
}
})
.exec((err, result) => {
if (err) return console.log(err);
console.log(result);
});
您必须将嵌套参考的嵌套填充到文档中。