0
У меня есть этот документ:заселить в мангуст-документах
[
{
_id: "54d278b2b6d57eee9f2c6d02",
title: "Piping",
mainCategories: [
{
title: "Shut valves",
subCategories: [
{
title: "Ball valve",
userCodeSyntax: "AV2",
typeComponents: [
"54d278b2b6d57eee9f2c6d00",
"54d278b2b6d57eee9f2c6d01"
]
}
]
}
]
}
]
Это категория схема, где typeComponents содержит список с продуктами в этой категории.
Моя модель:
var disciplineSchema = new Schema({
title: {type:String, required:true},
project:{
type: Schema.ObjectId,
ref: 'Project'
},
mainCategories:[
{
title: {type:String, required: true},
subCategories:[
{
title: {type:String, required: true},
userCodeSyntax: {type:String, required: true},
externalCode:String,
typeComponents:[ {type: Schema.ObjectId, ref: 'TypeComponent'}]
}
]
}
]
});
Можно ли заполнить typeComponents?
Я попытался это:
mongoose.model('Discipline').find()
.exec(function(err, disciplines){
var options = {
path: 'mainCategories.subCategories',
model: 'TypeComponent'
};
mongoose.model('Discipline').populate(disciplines, options, function (err, res) {
callback.status(200).send(res)
});
});
Каков интерес, имеющим «typeComponent объектом» в массиве «typeComponents» ? Вы должны просто иметь массив ObjectId типа typeComponents: ["54d278b2b6d57eee9f2c6d00", "54d278b2b6d57eee9f2c6d01"], так что вы сможете заполнить этот массив (http://mongoosejs.com/docs/populate.html), как описано в мангусте док. – jlouazel
@jlouazel Но как мне определить это в моей модели? Я не могу делать typeComponents [type: Schema.ObjectId, ref: 'TypeComponent'}]? – Per
@ PerStröm Вы можете: 'typeComponents: [{type: Schema.ObjectId, ref: 'TypeComponent'}]' – JohnnyHK