在nodejs中使用$ or运算符简单查找查询

时间:2019-04-23 10:10:28

标签: node.js mongodb express

我尝试使用mongodb数据库并通过with或operator进行简单查找来查找数据。

错误:-

  

获取结果时出错MongoError:未知运算符:$ or

主席表数据:-

 _id: 1
is_chairman: "1"
role: "2"
name: "amit"
mobile_no: 74056072323
password: "$2a$10$VqCa4hoTefkrmKSvYsI.GOa/lghnGDk74c7Z.1DMeVmqp/q2yH3.S"
society_name: "Jay Somnath"
created_at: 2019 - 04 - 23 07: 52: 02.021
updated_at: 2019 - 04 - 23 07: 52: 02.021

_id: 2
is_chairman: "2"
role: "2"
name: "karan raval"
mobile_no: 906712323233
password: "$2a$10$SMI374aSyLwAoEZJEvzzDOH0qJ6p4dqd47.jxUjPFbUTQczsWSeUS"
society_name: "pusph"
created_at: 2019 - 04 - 23 08: 14: 56.210
updated_at: 2019 - 04 - 23 08: 14: 56.211

_id: 3
is_chairman: "1"
role: "2"
name: "manthan"
mobile_no: 906545454
password: "$2a$10$SMI374aSyLwAoEZJEvzzDOH0qJ6p4dqd47.jxUjPFbUTQczsWSeUS"
society_name: "Jay"
created_at: 2019 - 04 - 23 08: 14: 56.210
updated_at: 2019 - 04 - 23 08: 14: 56.211

_id: 4
is_chairman: "2"
role: "2"
name: "mukesh"
mobile_no: 9043435676
password: "$2a$10$SMI374aSyLwAoEZJEvzzDOH0qJ6p4dqd47.jxUjPFbUTQczsWSeUS"
society_name: "krishnakung"
created_at: 2019 - 04 - 23 08: 14: 56.210
updated_at: 2019 - 04 - 23 08: 14: 56.211

我尝试这个:-

searchStr =  {"$or": [{'name':'jay'},
                      {'society_name':'jay'}
                        ]}
Chairman.find({is_chairman: "1"},searchStr,function(err,c){
     console.log(c);
}

我想要这种类型的结果

 is_chairman == 1 and $or: [{'name':'jay'},{'society_name':'jay'}]

1 个答案:

答案 0 :(得分:0)

对于不区分大小写的搜索,您可以尝试[as]

Chairman.find({
    $and: [
        {"is_chairman" : "1"},
        { $or: [
                {'name': /^jay$/i },
                {"society_name" : /^jay$/i }
            ]
        }
    ]
})

对于区分大小写的搜索:

Chairman.find({
    $and: [
        {"is_chairman" : "1"},
        { $or: [
                {'name': "jay" },
                {"society_name" : "jay" }
            ]
        }
    ]
})