![](/img/trans.png)
[英]Find a Mongodb document if it contains an element in the array in mongoose
[英]MongoDB - Find document in which array contains any items in query array
我有架構
person = {
skill = [{
type: String
}]
name = { type : String}
}
我有skill
陣
skill = ['python', 'css']
我想要所有匹配技能陣列中至少一項技能的人。
$all
和$in
只檢索匹配skill
數組中所有技能的人,但我想要匹配skill
數組中至少一項技能的人。
您可以使用$setIntersection
。
$setIntersection
- 將輸入技能數組與skill
字段相交,並返回一個具有共同(相交)值的數組。$ne
- 使用 (1) 的結果過濾文檔不是空數組。db.collection.find({
$expr: {
$ne: [
{
$setIntersection: [
[
"python",
"css"
],
"$skill"
]
},
[]
]
}
})
您可以使用"$in"
來達到您的目的。 也許您之前嘗試過時遇到了其他問題。
db.collection.find({
"skill": {
"$in": [ "python", "css" ]
}
})
在mongoplayground.net上試試。
只是為了好玩,這是另一個使用mgodatagen配置生成集合的mongoplayground.net示例。 查詢是一樣的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.