A
A
Adel Khalitov2020-05-03 14:35:51
MongoDB
Adel Khalitov, 2020-05-03 14:35:51

How to sort objects in mongo?

There is some kind of random collection, for example:

[
   {_id: ..., ratingPosition: 1},
   {_id: ..., ratingPosition: 2},
   {_id: ... }, <== Без параметра ratingPosition
]


I need to display all documents, but initially ratingPosition from smallest to largest (1, 2, 3...) and only after missing ratingPosition fields.

By doing a sort of {ratingPosition: -1} the positions are sorted from largest to smallest, after elements with no values.
{ratingPosition: 1} initially elements without values, and then from smaller to smaller.

Here is a similar question and possible solutions.

There are solutions in my head, but they are too complicated for such a task:
db.getCollection("goodsTreeNodes").aggregate(
    [
        { 
            "$facet" : { 
                "outputField1" : [
                    { 
                        "$match" : { 
                            "ratingPosition" : { 
                                "$exists" : true
                            }
                        }
                    }, 
                    { 
                        "$sort" : { 
                            "ratingPosition" : 1.0
                        }
                    }
                ], 
                "outputField2" : [
                    { 
                        "$match" : { 
                            "ratingPosition" : { 
                                "$exists" : false
                            }
                        }
                    }, 
                    { 
                        "$sort" : { 
                            "name" : 1.0
                        }
                    }
                ]
            }
        }, 
        { 
            "$project" : { 
                "result" : { 
                    "$concatArrays" : [
                        "$outputField1", 
                        "$outputField2"
                    ]
                }
            }
        }, 
        { 
            "$unwind" : { 
                "path" : "$result", 
                "includeArrayIndex" : "arrayIndex", 
                "preserveNullAndEmptyArrays" : false
            }
        }, 
        { 
            "$replaceRoot" : { 
                "newRoot" : "$result"
            }
        }
    ], 
    { 
        "allowDiskUse" : false
    }
);

Please advise something sooner. Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Philipp, 2020-05-03
@adelkhalitov

You need to check for values.
https://docs.mongodb.com/manual/reference/operator...
Run 2 queries and combine the results, but not a great idea.
As an alternative - use aggregation and fill empty fields with zero and then sort.
In general, something is wrong with the application logic. The very presence of a sorting problem hints at a problem elsewhere.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question