我正在嘗試使用$type運算子選擇檔案。
這是我的收藏:
{id: 1, a: [{b: "", c: ""}]},
{id: 2, a: {b: "", c: ""}}
這是我的查詢:
db.collection.find( {a: { $type: "array"}} )
// It will return id: 1
db.collection.find( {a: { $type: "object"}} )
// It will return id: 2 AND id: 1
對于第二個查詢,我只想選擇物件型別。
https://mongoplayground.net/p/kp6UVzrUzjA
我做錯了嗎?
uj5u.com熱心網友回復:
每種型別都與包含該型別的至少一個成員的型別或陣列相匹配。看到這個
詢問
測驗代碼在這里
db.collection.find({
a: {
$not: {
$type: "array"
},
$type: "object"
}
})
uj5u.com熱心網友回復:
您還可以使用聚合管道:
db.collection.aggregate([
{
$match: {
$expr: {
$eq: [ { $type: "$a" }, "object" ]
}
}
}
])
$type(聚合)
與
$type根據 BSON 型別匹配陣列元素的查詢運算子不同,$type聚合運算子不檢查陣列元素。相反,當傳遞一個陣列作為其引數時,$type聚合運算子回傳引數的型別,即“陣列”。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/335965.html
