[英]Mongoose sub doc referencing another sub doc
I've got a customer
schema with two sub-doc sets; 我有一个带有两个子文档集的customer
模式; orders
, and children
, which looks like this: 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);
Now, my problem is, that the Customers.orders.child doesn't populate correctly when I do a simple find and populate: 现在,我的问题是,当我执行简单的查找并填充时,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);
});
Sample 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 is null
, but should be a populated document of the child which _id is given. orders.child为null
,但应为已填充_id的子项的文档。
How do I manage this? 我该如何处理?
Try this: 尝试这个:
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);
});
You have to nest populate for nested ref to docs. 您必须嵌套填充以获取对文档的嵌套引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.