2015-02-10 4 views
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) 
     }); 

    }); 
+0

Каков интерес, имеющим «typeComponent объектом» в массиве «typeComponents» ? Вы должны просто иметь массив ObjectId типа typeComponents: ["54d278b2b6d57eee9f2c6d00", "54d278b2b6d57eee9f2c6d01"], так что вы сможете заполнить этот массив (http://mongoosejs.com/docs/populate.html), как описано в мангусте док. – jlouazel

+0

@jlouazel Но как мне определить это в моей модели? Я не могу делать typeComponents [type: Schema.ObjectId, ref: 'TypeComponent'}]? – Per

+1

@ PerStröm Вы можете: 'typeComponents: [{type: Schema.ObjectId, ref: 'TypeComponent'}]' – JohnnyHK

ответ

1

Вы пропускаете .typeComponents часть пути заселить.

Это работал, когда я попытался его с доком и парой Docs в typecomponents коллекции с соответствием _id значения:

mongoose.model('Discipline').find() 
.exec(function(err, disciplines){ 
    var options = { 
    path: 'mainCategories.subCategories.typeComponents', 
    model: 'TypeComponent' 
    }; 

    mongoose.model('Discipline').populate(disciplines, options, function (err, res) { 
    callback.status(200).send(res) 
    }); 
}); 
Смежные вопросы