26

I can do it easily on mysql

select * from TABLE order by length(FIELD) asc

How can I do it on MongoDB?

5
  • 7
    stackoverflow.com/questions/14647644/… if you want it to be efficient, I'd store the length of the string and sort with that. Commented Mar 18, 2013 at 11:00
  • @MagnusTengdahl, I tried to do mapreduce to produce another collection with a new column to store the string length and then sort by that field. But the map reduce is not fast and take around 1000ms for 20,000 rows and I need to create a new collection everytime I do sorting.
    – Henry Liu
    Commented Mar 19, 2013 at 4:19
  • I second WiredPrairie's response - store the string length as a separate field in the document and sort on that.
    – shelman
    Commented Mar 19, 2013 at 16:58
  • probably a project and then sort
    – Abhi
    Commented Jul 19, 2014 at 3:31
  • Possible duplicate of Select string length in mongodb Commented Apr 6, 2017 at 12:01

2 Answers 2

27

MongoDB 3.4 introduces the $strLenCP aggregation operator that finally supports this. An example:

db.collection.aggregate(
    [
        {$project: {
            "field": 1,
            "field_length": { $strLenCP: "$field" }
        }},
        {$sort: {"field_length": -1}},
        {$project: {"field_length": 0}}
    ]
)
4
  • Can you show how its work in js code, i tried to do something similar but is doesn't work? Commented Feb 8, 2020 at 2:00
  • This worked like a charm; thanks @zohar (Though as a MongoDB beginner myself, it might've been nice to note that the "field" key must refer to an extant field in the document, and explain that the "field_length" key is customizable, since it's calculated on demand.) Commented Mar 24, 2020 at 19:42
  • 1
    Note the filed must exist otherwise it will cause "errmsg" : "$strLenCP requires a string argument, found: missing",
    – zhuguowei
    Commented Apr 15, 2020 at 2:46
  • If you want the whole document and not just a subset of the fields, I'd recommend the $addFields operator rather than the first $project operator, as it will retain all existing fields in addition to adding the new one. {$addFields: { "field_length": {$strLenCP: "$field"}}}
    – M. Justin
    Commented Jul 19, 2021 at 18:02
-3

suppose your schema is something like:

example = {_id: "XXX", text: "YYY"}

db.example.aggregate([
 {$project : {text : 1, length : {$size : "$text"}}}, 
 {$sort : {length : 1}}
]);

I think this will do the job, but only for mongo 2.6 and above

2
  • 5
    $size is for arrays, not strings, so that will not work.
    – Wrench
    Commented Jan 28, 2015 at 19:28
  • uncaught exception: aggregate failed: { "errmsg" : "exception: The argument to $size must be an Array, but was of type: String", "code" : 17124, "ok" : 0 }
    – David
    Commented Dec 16, 2015 at 23:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.