1

I want to have query like this :

This query will select one document in the friendrequest collection where either the sender_id equals "id1" AND receiver_id equals "id2" OR sender_id equals = "id2" AND receiver_id equals "id1"

So can anyone help me to build that query ?

select * from friendrequest where (sender_id = "id1" and receiver_id = "id2") OR (receiver_id = "id1" and sender_id = "id2") for mySql

Error:

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}]}
4
  • 1
    Sounds pretty straightforward; what are you trying that's giving you a syntax error? Commented Feb 25, 2017 at 17:43
  • actually I couldnt complete the querry it will end like this :) I'll edit my question@JohnnyHK Commented Feb 25, 2017 at 17:44
  • Looks like you posted the query you tried but I don't see the error the query gave you Commented Feb 25, 2017 at 17:47
  • it should return a result from query { result : null } @metame Commented Feb 25, 2017 at 17:49

1 Answer 1

1

Multiple terms in the same object are implicitly $and'ed, so it just needs to be:

friendRequests.findOne({$or:[
    {sender_id:req.body._id, receiver_id:req.body._uid2},
    {sender_id:req.body._uid2, receiver_id:req.body._id}
]})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.