2015-06-30 2 views
2

Я не могу заставить мангуста заполнить массив объектов.Manoose populate array

Схема выглядит следующим образом:

var topOrganisationsForCategorySchema = new mongoose.Schema({ 
    category: String, 
    topOrganisations: [{ 
    organisation: { 
     type: mongoose.Schema.Types.ObjectId, 
     ref: 'organisation' 
    }, 
    model: mongoose.Schema.Types.Mixed 
    }] 
}); 

module.exports = mongoose.model('topOrganisationsForCategory', topOrganisationsForCategorySchema); 

Я хотел бы все объекты в этой коллекции, заполненного множеством организаций.

Вот что я попробовал

TopOrganisationsForCategory 
    .find() 
    .exec(function(err, organisation) { 
    var options = { 
     path: 'topOrganisations.organisation', 
     model: 'organisation' 
    }; 

    if (err) return res.json(500); 
    Organisation.populate(organisation, options, function(err, org) { 
     res.json(org); 
    }); 
    }); 

var organisationSchema = new mongoose.Schema({ 
    name: String, 
    aliases: [String], 
    categories: [String], 
    id: { 
    type: String, 
    unique: true 
    }, 
    idType: String 
}); 

organisationSchema.index({ 
    name: 'text' 
}); 

module.exports = mongoose.model('organisation', organisationSchema); 

ответ

0

Вы близки, но пара отмечает:

  • Следующий код предполагает, что вы также заявление схемы/модели для Oranisation.
  • Я не уверен, что свойство model означает как опцию (которая была бы недействительной) или фактически является свойством topOrganisations.

Итак, я оставил model как это не должно вызывать никаких проблем, но имейте в виду, что если вы используете его в качестве опции он не делать то, что вы могли бы думать, что это.

// Assuming this schema exists 
var organisationSchema = new mongoose.Schema({...}); 

var topOrganisationsForCategorySchema = new mongoose.Schema({ 
    category: String, 
    topOrganisations: [{ 
    organisation: { 
     type: mongoose.Schema.Types.ObjectId, 
     ref: 'Organisation' // Model name convention is to begin with a capital letter 
    } 
    // Is `model` supposed to be for the ref above? If so, that is declared in the 
    // Organisation model 
    model: mongoose.Schema.Types.Mixed 
    }] 
}); 

// Assuming these model definitions exist 
var Organisation = mongoose.model('Organisation', organisationSchema); 
var TopOrganisationsForCategory = mongoose.model('TopOrganisationsForCategory', TopOrganisationsForCategorySchema); 

// Assuming there are documents in the organisations collection 

TopOrganisationsForCategory 
    .find() 
    // Because the `ref` is specified in the schema, Mongoose knows which 
    // collection to use to perform the population 
    .populate('topOrganisations.organisation') 
    .exec(function(err, orgs) { 
    if (err) { 
     return res.json(500); 
    } 

    res.json(orgs); 
    }); 
+1

У меня есть схема организации, называемая организацией, но она по-прежнему не работает. Он просто возвращает коллекцию без заполненных объектов и только идентификатор – TJF

+0

@TJF Можете ли вы опубликовать свой код модели/схемы? Это должно работать, если нет проблемы с согласованием именования. –

+0

уверен - сделайте спасибо за помощь! – TJF