I am new to MongoDB and I wanted to know whether it is possible to fetch the documents by Id in the order I have specified in the array. I am using official c# driver.
Problem Description
I have some documents in the collection like this
{
"_id" : "1",
"Name" : "Assignment 1"
}
to
{
"_id" : "100",
"Name" : "Assignment 100"
}
I have a array of Ids { "4", "19", "10", "6" }
and what I need is to fetch the documents specified in the same order as in the array.
These are the queries that I tried
{ "_id" : { "$in" : ["4", "19", "10", "6"] } }
{ "$or" : [{ "_id" : "4" }, { "_id" : "19" }, { "_id" : "10" }, { "_id" : "6" }] }
But both of these queries are returning the documents in the following order
But my expected result is
The only option in front of me right now is to make separate db requests for each elements in the array which I think is a bad option.
Can anyone of you please steer me on fetching the documents in the order that I have specified in the array. I am using MongoDB C# driver and expecting an answer based on C#.
Thanks,
Kapil