我的 mongodb 檔案包含CarData內部具有多個屬性的物件。
{
_id: ...,
Name: "",
CarData: {
carId: null,
ownerName: "",
ownerPhone: "",
AdditionalInfo:{
RefNr: ""
validDoc: true
}
}
}
我知道我可以查詢整個集合
{ "CarData": { $exists: true } }
但我不知道如何查詢整個集合
CarData exists and is not null
and ownerName != ""
and AdditionalInfo exists and != null
and AdditionalInfo.validDoc == true
uj5u.com熱心網友回復:
在 mongo 中,您無需在檢查內部屬性之前檢查欄位是否存在。我認為這對你來說應該足夠了。操場
db.collection.find({
"CarData.ownerName": {
"$ne": ""
},
"CarData.AdditionalInfo.validDoc": true
})
uj5u.com熱心網友回復:
根據您的規范獲取檔案。您必須使用MongoDB 提供的聚合。下面的查詢將滿足您的要求。
db.collection.aggregate([
{
$match: {
"$and": [
{
CarData: {
"$exists": true
}
},
{
CarData: {
"$ne": null
}
},
{
"CarData.ownerName": {
$ne: ""
}
},
{
$and: [
{
"CarData.AdditionalInfo": {
"$exists": true
}
},
{
"CarData.AdditionalInfo": {
"$ne": null
}
},
{
"CarData.AdditionalInfo.validDoc": true
}
]
}
]
}
}
])
上述查詢將給出具有匹配條件的檔案串列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/396455.html
標籤:MongoDB 猫鼬 mongodb-查询
