1

When I use a multiple sorting in aggregate method with mongo, results aren't sorting in the right way. This is my query :

db.MyCollection.aggregate(
{
    "$unwind": "$objects"
},
{
    "$lookup": {
        "from": "CollectionA",
        "localField": "objects.itemId",
        "foreignField": "_id",
        "as": "itemOne"
    }
},
{
    "$lookup": {
        "from": "CollectionB",
        "localField": "user_id",
        "foreignField": "id",
        "as": "users"
    }
},
{
    "$lookup": {
        "from": "CollectionC",
        "localField": "objects.itemName",
        "foreignField": "name",
        "as": "itemTwo"
    }
},
{
    "$addFields": {
        "item": {
            "$arrayElemAt": [
                "$itemOne",
                0
            ]
        },
        "user": {
            "$arrayElemAt": [
                "$users",
                0
            ]
        },
        "itemP": {
            "$arrayElemAt": [
                "$itemTwo",
                0
            ]
        }
    }
},
{
    "$addFields": {
        "itemName": {
            "$ifNull": [
                "$item.name",
                "$objects.itemName"
            ]
        },
        "userName": {
            "$concat": [
                "$user.firstname",
                " ",
                "$user.lastname"
            ]
        }
    }
},
{
    "$match": {
        "client_id": 2
    }
},
{
    "$skip": 1
},
{
    "$limit": 10
},
{
    "$project": {
       "date": "$objects.date",
       "state": "$objects.state"
    }
},
{
    "$sort": { 
        "objects.state": 1,
        "objects.date": 1,
    }
}

)

To precise: "date" field is Date type and "state" field is number type. If I use only one sort : result order is correct. But if I use 2 sorts, results are not order correctly. Have you got any ideas, why ?

3
  • What do you mean by "results are not ordered correctly" ? Please add the current result of your query, and the expected output Commented Nov 7, 2017 at 10:17
  • 2
    They don't sort correctly because you renamed the fields in $project. So it should be { $sort: { state: 1, date: 1 } }. Not really sure that you realize that only those fields are being returned though. It frankly does not make any sense to have that $project after all the $addFields stages to "add new fields" and then just discard them. So you do seem to be missing a few concepts here. Commented Nov 7, 2017 at 10:20
  • @NeilLunn Perfect ! I forgot the rename field with $project... Thanks you ! Commented Nov 7, 2017 at 10:33

1 Answer 1

1

As @Neil Lunn says : They don't sort correctly because you renamed the fields in $project. So it should be { $sort: { state: 1, date: 1 } }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.