我正在使用猫鼬。我有以下模型:
person = {
name: String,
projects: [
{
projectId: {
type:mongoose.Schema.Types.ObjectId,
required:true
},
userNotes: {
type: String
}
}
]
};
我希望能够在列表const projectIds = ['aafefaeiIE2afd', 'afeaf33afe','gg3r3a']
中找到所有具有至少一个带有projectId的项目的用户。我试图做这样的事情:
const results = Person.find({projects:{$exists:true,$contains:projectIds}});
但是显然这将不起作用,因为a)猫鼬没有$contains
和b)person.projects
是对象数组而不是字符串数组,我需要在对象projectId
。
如何让猫鼬执行我要查找的查询?
答案 0 :(得分:0)
我不确定这是否正是您要寻找的东西,但据我了解,这可能会有所帮助。
db.collection.find({
"projects": {
"$elemMatch": {
"projectId": {
"$in": [
"aafefaeiIE2afd" //<-- You can use your array here instead
]
}
}
}
})
答案 1 :(得分:0)