如何在另一个文档中的数组中填充文档?
How to populate a document inside an array inside another document?
抱歉,如果标题看起来很复杂...我想不出更好的描述方式。
我的真实案例符合以下方案:
集合 1:
const mongoose = require('mongoose');
const itemSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: { type: String, required: [true, 'Name is required.'] },
quantity: { type: Number, required: [true, 'Quantity is required.'] },
collection2: { type: mongoose.Schema.Types.ObjectId, ref: 'Collection2' }
}, { _id : false });
const collection1Schema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: { type: String, required: [true, 'Name is required.'] },
imagePath: { type: String, required: [true, 'Image is required.'] },
items: [itemSchema]
});
module.exports = mongoose.model('Collection1', collection1Schema);
注意:itemsSchema 位于 collection1 文件中(并且声明 no _id's) 因为它们只存在于 Collection1 模型中(考虑到 "quantity" 和我为简化而删除的其他字段)。其他地方不需要这个 itemsScheme 作为另一个 collection。
集合 2:
const mongoose = require('mongoose');
const collection2Schema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: { type: String, required: [true, 'Name is required.'], unique: true }
});
module.exports = mongoose.model('Collection2', collection2Schema );
注意:为简化起见,删除了其他属性(例如 'imagePath')。
现在,这是我正在尝试的查询 运行:
Collection1.find()
.populate({
path: 'items',
populate: {
path: 'collection2', model: 'Collection2'
}
})
.then(...)
.catch(...);
这是我在 运行 时收到的错误消息:
Error fetching collection1: Cast to ObjectId failed for value "{
name: 'an item name',
quantity: 750
}" at path "_id" for model "Collection1"
如果我只是 运行:
就会发生完全相同的错误
Collection1.find()
.populate('items')
.then(...)
.catch(...);
也许我不能 运行 .populate('items')
因为它没有声明模型。如果是这种情况,我如何在查询 collection1 时填充 collection2?同样,我不能考虑将项目存储在单独的 collection.
中
但是如果我运行:
Collection1.find()
.populate('collection2')
.then(...)
.catch(...);
我得到了项目,没有错误,但它没有填充 collection2。好吧,这对项目有意义,因为它们只是 collection1 中的一组属性。但是填充 collection2 呢?
Collection2 已经添加了一些文档,所有文档的 _ids 和其他字段都已填写。在控制器中,我设置 _id: new mongoose.Types.ObjectId()
,同时为 Collection1 和 Collection2 这两种情况创建一个新文档。
在 front-end 中,我为 Collection1 创建了一个新文档,我添加了项目,每个项目都有一个来自 Collection2 的文档,我保存了所有内容,没有错误.我还确认所有内容都已正确保存(collection1 有项目列表,每个项目都有一个对 collection2 的 _id 引用)。唯一的问题是在这个数组中填充 collection2。
我已经尝试用 _ids 重构所有内容(包括 itemScheme)并删除所有 collections 来测试它再次但没有成功。
我已经被这个问题困扰了大约三天了。
我应该为 populate 设置任何特殊的 属性 以使其适用于该特定结构吗?
提前致谢...
populate('items')
这将不起作用,因为 item 不是模型。
你想要的是:
Collection1.find()
.populate('items.collection2')
.then(...)
.catch(...);
这将在所有数组元素
中填充collection2
抱歉,如果标题看起来很复杂...我想不出更好的描述方式。
我的真实案例符合以下方案:
集合 1:
const mongoose = require('mongoose');
const itemSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: { type: String, required: [true, 'Name is required.'] },
quantity: { type: Number, required: [true, 'Quantity is required.'] },
collection2: { type: mongoose.Schema.Types.ObjectId, ref: 'Collection2' }
}, { _id : false });
const collection1Schema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: { type: String, required: [true, 'Name is required.'] },
imagePath: { type: String, required: [true, 'Image is required.'] },
items: [itemSchema]
});
module.exports = mongoose.model('Collection1', collection1Schema);
注意:itemsSchema 位于 collection1 文件中(并且声明 no _id's) 因为它们只存在于 Collection1 模型中(考虑到 "quantity" 和我为简化而删除的其他字段)。其他地方不需要这个 itemsScheme 作为另一个 collection。
集合 2:
const mongoose = require('mongoose');
const collection2Schema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: { type: String, required: [true, 'Name is required.'], unique: true }
});
module.exports = mongoose.model('Collection2', collection2Schema );
注意:为简化起见,删除了其他属性(例如 'imagePath')。
现在,这是我正在尝试的查询 运行:
Collection1.find()
.populate({
path: 'items',
populate: {
path: 'collection2', model: 'Collection2'
}
})
.then(...)
.catch(...);
这是我在 运行 时收到的错误消息:
Error fetching collection1: Cast to ObjectId failed for value "{
name: 'an item name',
quantity: 750
}" at path "_id" for model "Collection1"
如果我只是 运行:
就会发生完全相同的错误Collection1.find()
.populate('items')
.then(...)
.catch(...);
也许我不能 运行 .populate('items')
因为它没有声明模型。如果是这种情况,我如何在查询 collection1 时填充 collection2?同样,我不能考虑将项目存储在单独的 collection.
但是如果我运行:
Collection1.find()
.populate('collection2')
.then(...)
.catch(...);
我得到了项目,没有错误,但它没有填充 collection2。好吧,这对项目有意义,因为它们只是 collection1 中的一组属性。但是填充 collection2 呢?
Collection2 已经添加了一些文档,所有文档的 _ids 和其他字段都已填写。在控制器中,我设置 _id: new mongoose.Types.ObjectId()
,同时为 Collection1 和 Collection2 这两种情况创建一个新文档。
在 front-end 中,我为 Collection1 创建了一个新文档,我添加了项目,每个项目都有一个来自 Collection2 的文档,我保存了所有内容,没有错误.我还确认所有内容都已正确保存(collection1 有项目列表,每个项目都有一个对 collection2 的 _id 引用)。唯一的问题是在这个数组中填充 collection2。
我已经尝试用 _ids 重构所有内容(包括 itemScheme)并删除所有 collections 来测试它再次但没有成功。
我已经被这个问题困扰了大约三天了。
我应该为 populate 设置任何特殊的 属性 以使其适用于该特定结构吗?
提前致谢...
populate('items')
这将不起作用,因为 item 不是模型。
你想要的是:
Collection1.find()
.populate('items.collection2')
.then(...)
.catch(...);
这将在所有数组元素
中填充collection2