【问题标题】:Mongoose sub doc referencing another sub docMongoose 子文档引用了另一个子文档
【发布时间】:2017-09-28 17:06:07
【问题描述】:

我有一个带有两个子文档集的 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 的孩子的填充文档。

我该如何管理?

【问题讨论】:

  • 你应该使用$lookup 而不是stackoverflow.com/questions/9621928/…
  • @sidgate $lookup 是否要求我使用聚合而不是查找?
  • 是的,需要聚合
  • @sidgate 好的,谢谢!
  • 嗯,我不确定这对子文档和子子文档有何帮助。或者我不明白聚合是如何工作的。

标签: javascript node.js mongodb mongoose-schema mongoose-populate


【解决方案1】:

试试这个:

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

您必须为嵌套的文档嵌套填充填充。

【讨论】:

    猜你喜欢
    • 2016-12-14
    • 1970-01-01
    • 2015-10-31
    • 2018-02-13
    • 2014-09-11
    • 1970-01-01
    • 2016-04-13
    • 2017-06-13
    • 2019-01-08
    相关资源
    最近更新 更多