我有一個這樣的模型:
var organization = new Schema({
things:[
{a:{
b:string
}
}
]
})
我正在嘗試findOne像這樣使用貓鼬:
let organization = await Organization.findOne({
"things.a.b": "uniquestring",
});
但問題是它a可能是空的。
我想做這樣的事情:
let organization = await Organization.findOne({
"things.a?.b": "uniquestring",
});
有沒有辦法在這個查詢中檢查 null ?
uj5u.com熱心網友回復:
我不確定這是否能回答你的問題,但你不能做這樣的事情。如果你認為你唯一的問題是可能有一些null物件
假設您的物件看起來像這樣
[
{
"things": [
{
"a": {
"b": "test"
}
},
{
"a": null
},
{
"a": {
"b": "abc"
}
}
]
}
]
然后你可以運行這個查詢
db.collection.find({
things: {
"$elemMatch": {
"a.b": "test"
}
}
},
{
"things.$": 1
})
你可以在這里查看 MongoDB 游樂場
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/450013.html
上一篇:更新檔案陣列中的專案
