【发布时间】:2019-08-26 06:32:34
【问题描述】:
一个列表可以有很多项目。我可以轻松地从列表中检索所有项目,但我遇到了相反的问题,即检索包含项目的所有列表
项目架构:
const ItemSchema = mongoose.Schema({
name: { type: String, required: true, min: 1 },
created_at: { type: Date, default: Date.now }
},{ toJSON: { virtuals: true }});
列表架构:
const ListSchema = mongoose.Schema({
title: { type: String, required: true, max: 100 },
user: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
description: { type: String, required: true },
items: [{
type: mongoose.Schema.Types.Mixed, ref: 'Item', quantity: 'String'
}],
completed: { type: Boolean, default: false },
date: { type: Date, default: Date.now },
});
文档:
"items": [
{
"_id": "5c6d74a98a3f532b4c1d2a23",
"quantity": "7"
}
],
我如何填充:Item.findById(id).populate('lists'); 但它返回空数组。
有什么建议吗?
【问题讨论】:
标签: node.js mongoose schema virtual populate