如何在CouchDB中使用$or运算符?
我正在尝试查询CouchDB,如下所示如何在CouchDB中使用$or运算符?,couchdb,Couchdb,我正在尝试查询CouchDB,如下所示 [{ "selector": { "docType": "student_details", { $or: ["studentName": { "$regex": "John" }, "studentAge": { "$regex": "15" }] } }
[{
"selector": {
"docType": "student_details",
{
$or: ["studentName": {
"$regex": "John"
}, "studentAge": {
"$regex": "15"
}]
}
}
}]
但它不起作用。索引是在“studentName”和“studentAge”上创建的。如果表达式中的任何一个有结果,我将尝试获取输出
但是,下面的查询是有效的
[{
"selector": {
"docType": "student_details",
"studentName": {
"$regex": "John"
}
}
}]
这给了我一个John的
studentName
的输出,看起来你的第一个JSON格式无效。CouchDB还希望运算符用双引号括起来,但是$或缺少它们
如果寻找相等,则不需要使用$regex
,而$eq
运算符更适合。选择器
可以按如下方式编写
"selector": {
"docType": "student_details",
"$or": [
{
"studentName": {
"$eq": "John"
}
},
{
"studentAge": {
"$eq": 15
}
}
]
}
或者更简单地使用隐式运算符(如果您真的必须使用$regex
,则这不是一个选项)
请注意,此解决方案假定studentAge
类型为number
嗨,Uminder,我之所以使用正则表达式是因为我将使用这个查询来实现搜索功能。所以如果我搜索“ohn”而不是“John”,它应该会给出相同的结果。无论如何,感谢您的帮助,$regex查询工作正常,看起来我在启动查询时出现了语法错误。
"selector": {
"docType": "student_details",
"$or": [
{ "studentName": "John" },
{ "studentAge": 15 }
]
}