Mongoose.js:是否可以按条件填充数组项?

时间:2015-02-08 11:38:35

标签: mongodb mongoose

我有这个架构:

    var PostSchema = new Schema({
    by: { type: mongoose.Schema.Types.ObjectId, ref:'user' },
    body: String,
    likes: [{ by: { type: mongoose.Schema.Types.ObjectId, ref:'user' }, isAnonymous: { type: Boolean, default: false}}],
});

此架构支持匿名赞。

我的问题:是否有办法只填充“喜欢”中的项目。数组,其中isAnonymous等于false?

1 个答案:

答案 0 :(得分:1)

是的,有

opts={ 
        path: 'likes', 
        match: { 
                isAnonymous:false
        },
        select: '<fields to be fetched>'
};

Post
    .find({})
    .populate(opts)

docs

help