我有一個具有以下屬性的 mongo 檔案
{
"label": [
"ibc",
"ibd",
"ibe"
],
"location": "vochelle st"
}
只有當檔案標簽與給定陣列完全匹配時,我才必須回傳檔案,即 ["ibc","ibd"] 并且同樣,我正在使用查詢
db.collection.find({"location":"vochelle st","dock_label":{"$all":["ibc", "ibd"]}})
實際反應:
{
"label": [
"ibc",
"ibd",
"ibe"
],
"location": "vochelle st"
}
預期反應:
{}
由于給定陣列中不存在標簽“ibe”,因此預期結果必須是空字典。
uj5u.com熱心網友回復:
提供$size您的查詢
db.collection.find({
location: "vochelle st",
label: {
$all: [
"ibc",
"ibd"
],
$size: 2
}
})
mongoplayground
uj5u.com熱心網友回復:
- 用于與輸入陣列
$setIntersection相交。label - 比較相交陣列(從 1 開始)和
label陣列通過$eq.
db.collection.find({
"location": "vochelle st",
$expr: {
$eq: [
{
$setIntersection: [
"$label",
[
"ibc",
"ibd"
]
]
},
"$label"
]
}
})
示例 Mongo Playground
uj5u.com熱心網友回復:
如果要檢查陣列是否與您的輸入完全匹配,則不需要任何運算子,只需將其與您的值進行比較:
db.collection.find({"location":"vochelle st","label": ["ibc", "ibd"]})
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/449264.html
上一篇:MongoDB聚合查找讓變數問題
