如何在CouchDB中使用$ or运算符?

问题描述 投票:0回答:1

我正在尝试像下面那样在CouchDB上进行查询

[{
    "selector": {
        "docType": "student_details",
        {
            $or: ["studentName": {
                "$regex": "John"
            }, "studentAge": {
                "$regex": "15"
            }]
        }
    }
}]

但是它不起作用。在“ studentName”和“ studentAge”上创建索引。如果任何一个表达式都有结果,我都在尝试获取输出。

但是以下查询有效,

[{
    "selector": {
        "docType": "student_details",
        "studentName": {
            "$regex": "John"
        }
    }
}]

[这给了我约翰的studentName的输出

couchdb
1个回答
0
投票

如果要查找完全匹配,则不需要使用$regex$eq运算符更适合。 selector可以编写如下。

"selector": { 
   "$or": [ 
      { 
         "studentName": { 
            "$eq": "John"
         }
      },
      { 
         "studentAge": { 
            "$eq": 15
         }
      }
   ]
}

或更简单地使用隐式运算符。

"selector": { 
   "$or": [ 
       { "studentName": "John" },
       { "studentAge": 15 }
   ]
}

请注意,此解决方案假定studentAge的类型为number

© www.soinside.com 2019 - 2024. All rights reserved.