1

I would like to retrieve documents from the DB that match a manager Id and employee ids which comes in an array.

SELECT * FROM XXX WHERE MANAGER_ID=<manager_id> AND EMPLOYEE_ID IN [....]

Here my code:

var query = [
    {             
        "$and": [ 
            {"employee": { $in : employees}}, 
            {"manager": ObjectId(managerId)}
        ]
    }
];

But in actuality, the query returns all documents in the model. Please advise.

2
  • 1
    I think problem is with array []... It should be var query = { "$and": [ {"employee": { $in : employees}}, {"manager": ObjectId(managerId)} ] } Commented Aug 10, 2018 at 6:58
  • Thank you! it works, please post as answer. Commented Aug 10, 2018 at 7:08

2 Answers 2

2

In find query first parameter should be an object not an array

var query = {
  "$and": [
    { "employee": { $in : employees }},
    { "manager": ObjectId(managerId) }
  ]
}

db.collection.find(query)
Sign up to request clarification or add additional context in comments.

Comments

0

use elemMatch to get matching the array's one or more keys

   ArrayFieldName: {
                "$elemMatch": {
                    "employee": {
                        $in: employees
                    },
                    "manager": ObjectId(managerId)
                }
     }

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.