skip to Main Content

I want sort in mongodb like "order by FIELD (id, 1,3,7,8)" of mysql.

My data mongo:

* Id1: 6281c8499f2dd7e9719a780d, name : "aaa",
* Id2: 6112c8499f2dd7e9719a780d, name : "asc",
* Id3: 628dd1b4fc0fd54971b87e17, name : "123"
* ...

Code:

db.getCollection("experts").find(
    {
        "_id" : {
            "$in" : [
                ObjectId("628dd1b4fc0fd54971b87e17"),
                ObjectId("6281c8499f2dd7e9719a780d"),
                ObjectId("6112c8499f2dd7e9719a780d"),
               ....
            ]
        }
    }
).sort( ???? );

Obtained result:

"628dd1b4fc0fd54971b87e17", 
"6281c8499f2dd7e9719a780d",
"6112c8499f2dd7e9719a780d"

Expected result:

* Id3: 628dd1b4fc0fd54971b87e17, name : "123"
* Id1: 6281c8499f2dd7e9719a780d, name : "aaa"
* Id2: 6112c8499f2dd7e9719a780d, name : "asc"

Dont sort field name I need sort field ID allow position array $in

2

Answers


  1. Chosen as BEST ANSWER

    Thanks all,

    I think this solution. And work :D

    db.getCollection("experts").aggregate(
        [
            {
                "$match" : {
                    "_id" : {
                        "$in" : [
                            ObjectId("628490ec4995322a26201e2a"),
                            ObjectId("6281c8499f2dd7e9719a780d"),
                            ObjectId("6112c8499f2dd7e9719a780d")
                        ]
                    }
                }
            }, 
            {
                "$project" : {
                    "_id" : 1.0,
                    "index" : {
                        "$indexOfArray" : [
                            [
                                ObjectId("628490ec4995322a26201e2a"),
                                ObjectId("6281c8499f2dd7e9719a780d"),
                                ObjectId("6112c8499f2dd7e9719a780d")
                            ],
                            "$_id"
                        ]
                    }
                }
            }, 
            {
                "$sort" : {
                    "index" : NumberInt(1)
                }
            }
        ]
    );
    

  2. As stated in my comment, this is not possible due to the nature of JSON documents. However, you can use a workaround like this:

    db.collection.aggregate([
       { $project: { data: { $objectToArray: "$$ROOT" } } },
       {
          $set: {
             data: {
                $sortArray: { input: "$data", sortBy: { k: 1 } }
             }
          }
       },
       {
          $set: {
             // positions start at index 0
             sortKey1: { $arrayElemAt: ["$data", 0] },
             sortKey2: { $arrayElemAt: ["$data", 2] },
             sortKey3: { $arrayElemAt: ["$data", 6] },
             sortKey4: { $arrayElemAt: ["$data", 7] }
          }
       },
       {
          $sort: {
             "sortKey1.v": 1,
             "sortKey2.v": 1,
             "sortKey3.v": 1,
             "sortKey4.v": 1
          }
       },
       { $replaceWith: { $arrayToObject: "$data" } }
    ])
    

    Mongo Playground

    Note, usually it is a bad design to store numeric values as string. Check Ascending/Descending Sort whether the sorting algorithm matches your requirements.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search