我正在尝试填充数组数组。模式如下:
const IngredientAttachment = {
ingredient: { type: mongoose.Schema.Types.ObjectId, ref: Ingredient.modelName, required: true },
// there's more fields here
};
const mealSchema = new Schema({
ingredients: [[ IngredientAttachment ]] // array of array of attachments
// there's more fields here
});
一个未填充的Meal
文档示例如下:
{
"ingredients": [
[{
"_id": "5bcf9b3f2e33ad15f52bd831",
"ingredient": "5bce60f074c12923c589db90"
},
{
"_id": "5bcf9c94652b8f164f6a2566",
"ingredient": "5bce85b76bb8812b2eb5322c"
}
]
]
}
我试图用这个:
meal = await meal
.populate({
path: 'ingredients',
populate: {
path: 'ingredient',
}
})
但是那什么也没做:)
谢谢!
编辑:目前,我通过引入字段options
“解决”了该问题:
ingredients: [{
options: [ IngredientAttachment ]
}]
在我需要将膳食作为一系列食材的地方,我将其像这样转换:
meal.ingredients = meal.ingredients.map(m => m.options);
答案 0 :(得分:0)
尝试这种方式
meal.find({}).populate('ingredients.ingredient').exec(...)
答案 1 :(得分:0)
在解决问题的下方。
成分架构文件:
const mongoose = require('mongoose');
const ingredient = new mongoose.Schema({
name: {
type: String,
required: true
}
});
module.exports = mongoose.model('Ingredient', ingredient);
成分附件架构文件:
const mongoose = require('mongoose');
const ingredientAttachment = new mongoose.Schema({
ingredient: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Ingredient',
required: true
},
description: {
type: String,
required: true
}
});
module.exports = mongoose.model('IngredientAttachment', ingredientAttachment);
膳食模式文件:
const mongoose = require('mongoose');
const mealSchema = new mongoose.Schema({
ingredients: {
type: [[
{
type: mongoose.Schema.Types.ObjectId,
ref: 'IngredientAttachment',
required: true
}
]]
},
type: {
type: String,
required: true
}
});
module.exports = mongoose.model('Meal', mealSchema);
这是我用于测试的代码:
const ingredientA = new ingredientSchema({name: 'ingredientA'});
await ingredientA.save();
console.log(`ingredientA=${ingredientA}`);
const ingredientB = new ingredientSchema({name: 'ingredientB'});
await ingredientB.save();
console.log(`ingredientB=${ingredientB}`);
const ingredientC = new ingredientSchema({name: 'ingredientC'});
await ingredientC.save();
console.log(`ingredientC=${ingredientC}`);
const ingredientD = new ingredientSchema({name: 'ingredientD'});
await ingredientD.save();
console.log(`ingredientD=${ingredientD}`);
const ingredientAttachmentA = new ingredientAttachmentSchema({ingredient: ingredientA, description: 'descriptionA'});
await ingredientAttachmentA.save();
console.log(`ingredientAttachmentA=${ingredientAttachmentA}`);
const ingredientAttachmentB = new ingredientAttachmentSchema({ingredient: ingredientB, description: 'descriptionB'});
await ingredientAttachmentB.save();
console.log(`ingredientAttachmentB=${ingredientAttachmentB}`);
const ingredientAttachmentC = new ingredientAttachmentSchema({ingredient: ingredientC, description: 'descriptionC'});
await ingredientAttachmentC.save();
console.log(`ingredientAttachmentC=${ingredientAttachmentC}`);
const ingredientAttachmentD = new ingredientAttachmentSchema({ingredient: ingredientD, description: 'descriptionD'});
await ingredientAttachmentD.save();
console.log(`ingredientAttachmentD=${ingredientAttachmentD}`);
const meal = new mealSchema({
ingredients: [[ingredientAttachmentA, ingredientAttachmentB], [ingredientAttachmentC, ingredientAttachmentD]],
type: 'spicy'
});
await meal.save();
console.log(`meal=${meal}`);
const mealPopulated = await mealSchema.find({_id:meal._id}).populate({
path: 'ingredients',
model: 'IngredientAttachment',
populate: { path: 'ingredient',
model: 'Ingredient' }
}).exec();
console.log('------ mealPopulated ------');
console.log(JSON.stringify(mealPopulated, null, 0));
这是执行后的输出:
填充json格式的