是否可以在地图架构类型中填充对象? [英] Is it possible to populate objects in map schema type?
本文介绍了是否可以在地图架构类型中填充对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的猫鼬模型中有模式类型 Map.在此映射中,每个元素都引用了另一个模型.我知道可以在数组中填充属性,但是Map类型呢?导致像"map_type_attribute.some_attribute_to_populate"这样的嵌套;不起作用.:)
I have schema type Map in my mongoose model. In this map, each element has reference to another model. I know that it's possible to populate attributes in array, but how about Map type? Be cause nesting like "map_type_attribute.some_attribute_to_populate" doesn't work. :)
这是我的模特
const Mongoose = require('mongoose');
const parameter = Mongoose.Schema({
definition: {
type: Mongoose.Schema.Types.ObjectId,
ref: 'Definition',
},
value: {},
}, {_id: false});
const schema = Mongoose.Schema({
model: {
type: Mongoose.Schema.Types.ObjectId,
ref: 'Model'
},
name: String,
objectid: Number,
externalId: String,
properties: Mongoose.Schema.Types.Mixed,
parameters: {
type: Map,
of: parameter
}
});
module.exports = Mongoose.model('Element', schema);
这是我尝试填充定义字段的方式:
This is how i'm trying to populate definition field:
const request = Element.find(query, projection);
request.populate('parameters.definition');
request.exec( (err, docs) => {
...
推荐答案
此功能已已添加(猫鼬5.10.3)(2020年9月).您只需用 $ *
表示地图中的每个元素.
This functionality was added in Mongoose 5.10.3 (September 2020). You simply denote every element in the map with a $*
.
在您的示例中,这将是:
In your example this would be:
const request = Element.find(query, projection);
request.populate('parameters.$*.definition');
request.exec( (err, docs) => {
这篇关于是否可以在地图架构类型中填充对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文