0

Problem: I am trying to fetch data from the MongoDB database as per the defined in an Array of ObjectId's. While fetching the data I have noticed that the data isn't ordered as per the defined ObjectId in the Array field.

module_id: [
  ObjectId('60f65202cfq57631f324061b'),
  ObjectId('60f66df8cfx57631f324061e'),
  ObjectId('60f66bedcfv57631f324061d'),
  ObjectId('610262e58eu2031d264e934b'),
  ObjectId('611a143bf66m223dd5338641'),
  ObjectId('6119efdf4770b836e1ceaf24')
]

Desired Output: I wanted the fetched result should be as per the array ids and I am passing the same order that is defined above but getting some random order result. Don't want to be by default sort.

To fetch the result using .find({ _id: { $in: module_id }}).toArray(function (err, result) {console.log(result)})

Does anybody have any idea where I am doing wrong?

1 Answers1

0

Mongo, find through list of ids

you should loop through array

im try this and work fine in mongo shell

let ids=["6116b8175d761433233e1719","6116b8175d761433233e171a"]
 let objIds=ids.map((id)=>ObjectId(id))
 db.users.find({_id:{$in:objIds}})
majid
  • 404
  • 4
  • 8