MongoDB Collection (User) 中有一個名為“name”的子欄位:
[
{
"teacher": {
"name": "Alex",
"email": "[email protected]"
},
"waiter": {
"name": "Feliks",
"email": "[email protected]"
},
"pilot": [
{
"name": "Sam",
"email": "[email protected]"
},
{
"name": "alice",
"email": "[email protected]"
}
],
},
{
"teacher": {
"name": "max",
"email": "[email protected]"
},
"waiter": {
"name": "Sam",
"email": "[email protected]"
},
"pilot": [
{
"name": "richard",
"email": "[email protected]"
},
{
"name": "alice",
"email": "[email protected]"
}
],
}
]
如何根據欄位“名稱”查找資料。例如,當我在尋找 'Sam' 時,它應該回傳所有檔案,因為 'Sam' 分別在第一個和第二個檔案中的“服務員”和“飛行員”中。
我不能做這樣的事情:
User.find({"teacher.name": "Sam", "waiter.name": "Sam", "pilot.name": "Sam" })
這不會給我任何回報,因為它是一個 AND 邏輯。我需要的是一個 OR 邏輯。
uj5u.com熱心網友回復:
您可以使用$or運算子。所以查詢應該是這樣的:
User.find({ $or: [
{ "teacher.name": "Sam" },
{ "waiter.name": "Sam" },
{ "pilot.name": "Sam" }
]
});
在這里閱讀我的詳細資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/384744.html
