Mongodb nodejs查询语法错误

时间:2017-02-25 17:41:10

标签: node.js mongodb mongodb-query

我想要这样的查询:

此查询将在friendrequest集合中选择一个文件,其中sender_id等于“id1”AND receiver_id等于“id2”或者sender_id等于=“id2”AND receiver_id等于“id1”

那么有人可以帮我构建那个查询吗?

从friendrequest中选择*其中(sender_id =“id1”和receiver_id =“id2”)或者(receiver_id =“id1”和sender_id =“id2”)for mySql

错误:

friendRequests.findOne({$or:[{$and:[{sender_id:req.body._id,receiver_id:req.body._uid2}],$and:[{sender_id:req.body._uid2,receiver_id:req.body._id}]}

1 个答案:

答案 0 :(得分:1)

同一个对象中的多个术语隐式为$和' ed,因此它只需要:

friendRequests.findOne({$or:[
    {sender_id:req.body._id, receiver_id:req.body._uid2},
    {sender_id:req.body._uid2, receiver_id:req.body._id}
]})