填充文档子数组的mongoose子数组

时间:2015-02-11 16:34:30

标签: mongodb mongoose mongoose-populate

我有一个Roommate Group mongoose Schema,

var RoommateGroupSchema = new Schema({

    roommates: [ { type: Schema.ObjectId, ref: "User" } ]

});

我有一个用户mongoose Schema,

var UserSchema = new Schema({

    name: String

    , billers: [{
        utilityProvider: { type: Schema.ObjectId, ref: 'UtilityProvider' }
        , fields: String
    }]

});

我有一个Utility Provider mongoose Schema,

var UtilityProviderSchema = new Schema({

   name: String

});

我想查询我的室友组架构并填充所有子阵列,所以我得到这样的结果。

roommateGroup = {

    roommates: [
        {
            name: 'Sean Hill'
            , billers: [
                {
                    utilityProvider: {
                        name: 'Google Fiber'
                    }
                    , fields: 'Internet'
                },
                {
                    utilityProvider: {
                        name: 'Comcast'
                    }
                    , fields: 'Internet'
                }

            ]
        },
        {
            name: 'Johnny Lingo'
            , billers: ...
        }
    ]

}

我已经尝试了

RoommateGroupModel
    .findById(groupId)
    .populate({ path: 'roommates' })
    .exec(function(err, group){

        RoommateGroupModel.populate(group, { path: 'roommates.billers', model: 'UtilityProvider' }, function(err, populatedGroup){
            // But the billers.utilityProvider for each index in the array isn't populated, it's just the _id.
        });

    })
;

任何帮助都会很棒!

0 个答案:

没有答案