Sort the mongo db collection based on values

I have a mongo db collection as described below:
({ ‘name’ : ‘rohan’, ‘dept’: ‘cse’ } ,
{ ‘name’ : ‘aakash’, ‘dept’: ‘mech’ },
{ ‘name’ : ‘kiara’, ‘dept’: ‘cse’ } )

Query: I have a list x = [mech, cse] which means that I want to see all dept=mech first and then cse and so on. How can I sort my collection based on the values ?

Output: My collection should look like:
({ ‘name’ : ‘aakash’, ‘dept’: ‘mech’ },
{ ‘name’ : ‘rohan’, ‘dept’: ‘cse’ } ,
{ ‘name’ : ‘kiara’, ‘dept’: ‘cse’ } )

Hi @nancy_tyagi ,

It I understand correctly the challenge is thet you need to respect the order of the values in the array but you don’t know the values before hand and cannot sort the input array.

For example you can have ["x", "y"] or ["y","x"] and based on the order you need to sort.

In the case I can give you a high level guidance and maybe later on I can give you an example.

You should have a pipeline in aggregation that first match the array values to get all the related documents, then you need to use $addFields + $indexOfArray for each document “$x” in the input array to get a new field with the index number. Final stage should be sort based on that newly added placement field to get the order you want…

$addFields :{ placement : 
{ $indexOfArray: [ ["mech", "cse"] , "$x" ] }}

Hope you find this helpful

Thanks
Pavel

Hi @Pavel_Duchovny ,

You understood the problem statement correctly. I need the sort based on the elements in the list. So if “x” comes first then I need all the documents with the value “x” first.

But since I am new to mongo db, Can you help me with the exact statement to be implemented?

Thanks a lot!!! :slight_smile:

Hi @nancy_tyagi ,

I believe it should look something of the following sort, but I haven’t tested it:

db.collection.aggregate([
{
 $match : { x :  ["mech", "cse"]}
},
{
$addFields :{ placement : 
{ $indexOfArray: [ ["mech", "cse"] , "$x" ] }}
},
{$sort : { placement : 1 } }
// Optimally add : , {$project : {  placement : 0 }}
]

Thanks
Pavel

Hi @Pavel_Duchovny

I tried to test the solution, but it seems the sort is not happening. After I implemented your solution, my collection still looks the same.

INPUT COLLECTION:
({ ‘name’ : ‘rohan’, ‘dept’: ‘cse’ } ,
{ ‘name’ : ‘aakash’, ‘dept’: ‘mech’ },
{ ‘name’ : ‘kiara’, ‘dept’: ‘cse’ } )

and even the output collection is same. Can you please help me with it?

Hi @nancy_tyagi ,

Sorry mistakenly used some wrong field names. The following works for me:

db.collection.insertMany([{ "name" : "rohan", "dept": "cse" } ,
{ "name" : "aakash", "dept": "mech" },
{ "name" : "kiara", "dept": "cse" }]);

db.collection.aggregate([
{
 $match : { dept : { $in : ["mech", "cse"]}}
},
{
$addFields :{ "placement" : { $indexOfArray: [ ["mech", "cse"] , "$dept" ] }}
},
{$sort : { placement : 1 } }
]);


The result ireturned by query is as follows:

1 Like

Hi @Pavel_Duchovny

You have been a life saviour. Thanks a lot for your help!!! :blush:

1 Like

Any document reference on how to add fields (with indexOfArray expression)like this while using mongo-java driver.