Hello @Subham_Kumar_Sahu, Welcome to the MongoDB Community Forum,
The $in operator won’t preserve the order or documents in response as passed in the match.
You have to try an aggregation query with some additional stage for that operation, something like this, Consider _id is your matching property,
$matchstage to match your condition$addFieldsto introduce a new fieldindex$indexOfArrayto find the index of the current document’s_idproperty in your match array$sortto sort the result byindexfield in ascending order
var matchArray = [uuid5,uuid1,uuid2];
db.collection.aggregate([
{ $match: { _id: { $in: matchArray } } },
{
$addFields: {
index: {
$indexOfArray: [matchArray, "$_id"]
}
}
},
{ $sort: { index: 1 } }
])
Look at a similar question for more information,