0

I have a table with following values

[
{
   id: 1,
   name: "abc"
},
{
   id: 2,
   name: "lmn"
},
{
   id: 3,
   name: "xyz"
}
]

I have a query with $in as

{
  id: {
    $in: [ 2, 3, 1 ]
  }
}

i want the output to come like so:

[
{
  id: 2,
  name: "lmn"
},
{
  id: 3,
  name: "xyz"
},
{
  id: 1,
  name: "abc"
}
]

But it does not come like that. Is there a way I could achieve this output?

Melroy Fernandes
  • 371
  • 1
  • 5
  • 16

1 Answers1

1

You have to add a $sort stage, as $in does not preserve order.

There are quite a few questions on stackoverflow about this issue for example this question.

Tom Slabbaert
  • 21,288
  • 10
  • 30
  • 43