Mongoose populate不会返回所有数据

时间:2015-04-23 18:12:41

标签: node.js mongodb mongoose mongoose-populate

我有2个架构:

var countrySchema = new Schema({
    countryId: String,
    countryName: String,
    cities: [
        { type: Schema.Types.ObjectId, ref: 'City' }
    ]
});

var citySchema =  new Schema({
    cityId: String,
    cityName: String
});

在我的数据库中,我有3个国家和14个城市(4,4,6),3个国家成功返回,但在每个国家的城市阵列中只有1个城市。

这是我的填充代码:

Models.Country.find().populate('cities').exec(function(error, countries) {
    res.json(countries);
});

我做错了吗?

(来自Mongo Shell):

国家/地区收集结果:

{ "_id" : ObjectId("55351b50ebcef784a8b5ae84"), "countryId" : "argentina", "countryName" : "Argentina", "cities" : [ "5535136eebcef784a8b5ae76", "5535136eebcef784a8b5ae77", "5535136eebcef784a8b5ae78", "5535136eebcef784a8b5ae79" ] }
{ "_id" : ObjectId("55351bf7ebcef784a8b5ae85"), "countryId" : "chile", "countryName" : "Chile", "cities" : [ "553514baebcef784a8b5ae7a", "553514baebcef784a8b5ae7b", "553514baebcef784a8b5ae7c", "553514baebcef784a8b5ae7d" ] }
{ "_id" : ObjectId("55351c60ebcef784a8b5ae86"), "countryId" : "brazil", "countryName" : "Brazil", "cities" : [ "55351931ebcef784a8b5ae7e", "55351931ebcef784a8b5ae7f", "55351a0bebcef784a8b5ae80", "55351a0bebcef784a8b5ae81", "55351a0bebcef784a8b5ae82", "55351a0bebcef784a8b5ae83" ] }

城市收集结果:

{ "_id" : ObjectId("5535136eebcef784a8b5ae76"), "cityId" : "buenos_aires", "cityName" : "Buenos Aires", "hostels" : [ "55351194ebcef784a8b5ae71" ] }
{ "_id" : ObjectId("55351399ebcef784a8b5ae77"), "cityId" : "bariloche", "cityName" : "Bariloche", "hostels" : [ "553511b4ebcef784a8b5ae72" ] }
{ "_id" : ObjectId("553513beebcef784a8b5ae78"), "cityId" : "el_calafate", "cityName" : "El Calafate", "hostels" : [ ] }
{ "_id" : ObjectId("5535144bebcef784a8b5ae79"), "cityId" : "el_chalten", "cityName" : "El Chalten", "hostels" : [ ] }
{ "_id" : ObjectId("553514baebcef784a8b5ae7a"), "cityId" : "pucon", "cityName" : "Pucon", "hostels" : [ "553511c0ebcef784a8b5ae73" ] }
{ "_id" : ObjectId("553516e7ebcef784a8b5ae7b"), "cityId" : "santiago", "cityName" : "Santiago", "hostels" : [ ] }
{ "_id" : ObjectId("55351803ebcef784a8b5ae7c"), "cityId" : "punto_arenas", "cityName" : "Punto Arenas", "hostels" : [ ] }
{ "_id" : ObjectId("5535180cebcef784a8b5ae7d"), "cityId" : "punto_natales", "cityName" : "Punto Natales", "hostels" : [ ] }
{ "_id" : ObjectId("55351931ebcef784a8b5ae7e"), "cityId" : "rio_de_janero", "cityName" : "Rio De Janero", "hostels" : [ "553511c0ebcef784a8b5ae74", "553511c0ebcef784a8b5ae75" ] }
{ "_id" : ObjectId("55351943ebcef784a8b5ae7f"), "cityId" : "floranapolis", "cityName" : "Floranapolis", "hostels" : [ ] }
{ "_id" : ObjectId("55351a0bebcef784a8b5ae80"), "cityId" : "ita_care", "cityName" : "Ita Care", "hostels" : [ ] }
{ "_id" : ObjectId("55351a1cebcef784a8b5ae81"), "cityId" : "pipa", "cityName" : "Pipa", "hostels" : [ ] }
{ "_id" : ObjectId("55351a40ebcef784a8b5ae82"), "cityId" : "jericoacoara", "cityName" : "Jericoacoara", "dealers" : [ ] }
{ "_id" : ObjectId("55351a84ebcef784a8b5ae83"), "cityId" : "salvador", "cityName" : "Salvador", "hostels" : [ ] }
{ "_id" : ObjectId("55352028ebcef784a8b5ae87"), "cityId" : "lima", "cityName" : "Lima", "hostels" : [ ] }

0 个答案:

没有答案