我有這些檔案:
{"_id" : 1, "actors" : [{"matricule" : "AVB", "name":"XXX"}, {"matricule" : "AVB", "name":"YYY"}]}
{"_id" : 2, "actors" : [{"matricule" : "PMH", "name":"FFF"}, {"matricule" : "BNG", "name":"HHH"}]}
我只想獲取第一個檔案,因為它是相同的矩陣但名稱不同。
我使用這個查詢:
{
$expr:{$eq:["$actors.0.matricule", "$actors.1.matricule"]}
}
但它不起作用,我沒有任何結果。你知道為什么嗎 ?
uj5u.com熱心網友回復:
嘗試使用$arrayElemAt
db.collection.find({
$expr: {
$eq: [
{
"$arrayElemAt": [
"$actors.matricule",
0
]
},
{
"$arrayElemAt": [
"$actors.matricule",
1
]
}
]
}
})
這是Mongo游樂場供您參考。
uj5u.com熱心網友回復:
詢問
- "$actors.matricule" 是所有這些矩陣值的陣列
- 使其成為一個集合(與空陣列聯合)=> 沒有重復
- 過濾以僅保留大小為 1 的那些(都具有相同的值)
*如果您在actors陣列中有一個或多個成員,則可以作業
玩蒙哥
aggregate(
[{"$match":
{"$expr":
{"$eq": [{"$size": {"$setUnion": ["$actors.matricule", []]}}, 1]}}}])
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/465226.html
