我應該帶條件的資料:
(成立于 2004 年或成立于 10 月)和(社交或網路)
我曾經這樣嘗試:
db.companies.find({
$or: [
{"founded_year": 2004},
{"founded_month": 10}
],
$or: [
{"category_code": "web"},
{"category_code": "social"}
]
}).count()
但這是錯誤的。正確答案是:
db.companies.find({ "$and": [
{ "$or": [ { "founded_year": 2004 },
{ "founded_month": 10 } ] },
{ "$or": [ { "category_code": "web" },
{ "category_code": "social" }]}]}).count()
為什么要在內部 {} 中使用 $and 運算子?我以為我不必撰寫這個運算子,我可以用逗號列出 {} 中的條件,因為在 MongoDB 大學課程中,這里運算式:


uj5u.com熱心網友回復:
在查詢中
{
"$or": [
{"founded_year": 2004},
{"founded_month": 10}
],
"$or": [
{"category_code": "web"},
{"category_code": "social"}
]
}
你有兩個名為$or. 或者 mongo 查詢遵循 JSON 符號。而在 JSON 格式中,只會使用具有重復名稱的最后一個鍵。
使用$and將解決此問題。因為$and是一個串列,您將不再擁有包含兩個相同鍵的物件
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/359641.html
標籤:MongoDB
