1

I would like to query a collection called orders using the default _id with an array that has the _ids that I need but when i try to save the result in another

let orderIds = ['example1', 'example2']
db.collection("orders").find({ _id: { $all: orderIds } }, function (err, orders) {
    console.log(orders);
})
Nikhil Savaliya
  • 2,138
  • 4
  • 24
  • 45
Bryan
  • 141
  • 1
  • 9
  • $all expects the field to be queried to be an array, and it checks whether it contains all the elements. You're looking for the [$in operator](https://docs.mongodb.com/manual/reference/operator/query/in/). – Plancke Jun 14 '19 at 02:23

1 Answers1

0

You need to use $in instead of $all to get all the documents from a given array of ids

Try this:

db.collection("orders").find({ _id: { $in: orderIds } }, function (err, orders) {
    console.log(orders);
})

Read more about $in here

Ravi Shankar Bharti
  • 8,922
  • 5
  • 28
  • 52