0

I had list of ids in array

Employees array

let employeeIds= [
    ObjectId("5b0d4c5ec47e6223a08af5fd"),
    ObjectId("5b1625f762368179e1e4549c"),
    ObjectId("5b3a15979a68763230202dfd"),
    ObjectId("5b3b0ea9074f944699f1bcfc"),
    ObjectId("5b45eb1214e42414cc9a2191"),
    ObjectId("5b83e3fc6fcad70850159ae2"),
    ObjectId("5bdc5a8ab8d82616a54b5667"),
    ObjectId("5bdc5b43d9c22617406f5b4d"),
    ObjectId("5bdd3a9da9ad6b138b503d7d"),
    ObjectId("5bf7e3840c414b4a1612da36"),
    ObjectId("5c18d04256573a536a201599"),
    ObjectId("5c18d05b56573a536a2015a2"),
    ObjectId("5ba8c52a5e0e986f16102c6f"),
    ObjectId("5ba34247decd71414691021d"),
    ObjectId("5bdd3d0ccf9d1417166e47ec")
]

In my mongodb stores collection, there are list of employees in array

{
     "_id" : ObjectId("5b0d3fa6b426ea12ec0f6e5a"),
    "name" : "xxxx",
    "email" : "[email protected]",
    "telephone" : "24301212",
    "mobile" : "+91********",
    "employees" : [ 
        ObjectId("5b0d4c5ec47e6223a08af5fd"), 
        ObjectId("5b45eb1214e42414cc9a2191"), 
        ObjectId("5b3b0ea9074f944699f1bcfc"), 
        ObjectId("5b1625f762368179e1e4549c"), 
        ObjectId("5b83e3fc6fcad70850159ae2"), 
        ObjectId("5bdc5a8ab8d82616a54b5667"), 
        ObjectId("5bdc5b43d9c22617406f5b4d"), 
        ObjectId("5bdd3a9da9ad6b138b503d7d"), 
        ObjectId("5b3a15979a68763230202dfd"), 
        ObjectId("5bf7e3840c414b4a1612da36"), 
        ObjectId("5c18d04256573a536a201599"), 
        ObjectId("5c18d05b56573a536a2015a2")
    ],
}

I need to compare employees array in my store employees collection with mongodb query. I tried using find() the store & using lodash to took the employees who are all match in this store collections. How can I do with in this query? Can you please suggest any idea?

2
  • I am not sure if I understood correctly: Do you want to fetch all documents, which hold an array of employees which holds at least one of the ids referenced in employeeIds, or do you only want documents, where ALL of the ids in the employees array match ALL of the ids in employeeIds? Commented Dec 21, 2018 at 13:25
  • Match the employeeIds and store.employees then return who are all in the stores. Commented Dec 21, 2018 at 13:56

1 Answer 1

1

This will return all stores, where at least one of the employees is employed at.

store.find({
 employees :  {
  $elemMatch: 
   { 
    $in : [ ObjectId("5ba8c52a5e0e986f16102c6f"), ObjectId("5ba34247decd71414691021d")]
   }
 }
})
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.