在MongoDb中搜索数组的id

时间:2017-01-05 03:00:44

标签: node.js mongodb mongoose

我有这个HomeSchema

Schema = mongoose.Schema;


homeSchema = new Schema({
    name: { type: String, required: true},
    administrator: {type : mongoose.Schema.Types.ObjectId, ref: 'User', required: true},


users: [{ type : mongoose.Schema.Types.ObjectId, ref: 'User' }],
});

module.exports = mongoose.model('Home', homeSchema);

我想查询该用户数组中的特定用户是否基于user._id?我知道这很简单,但我该怎么做?我必须使用$ elematch吗?

1 个答案:

答案 0 :(得分:0)

您可以使用 $ in 。此查询将搜索您的集合中的users数组中存在的所有 userId (这应该是一个数组)。如果你想要用户的一些细节,你可以使用填充。

var query = {users : {$in:userId} }