Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sort by string length in Mongodb/pymongo

I was wondering if anyone knows how to sort a mongodb find() result by string length.

I have tried something like db.foo.find().sort({item.lenght:-1}) but obviously doesn't work. Can somebody help me and also suggest me a way to do the same thing but in pymongo?

like image 218
user1718064 Avatar asked Apr 07 '14 11:04

user1718064


2 Answers

There are lot of things ( and basic API ) I would personally love to see in the aggregation framework such as:

Math functions

  • log (as in logarithm)
  • ceil
  • floor

Array

  • sum

String

  • length

Just to name a few.

And that is without resorting to obscure usages of the $mod operator or other means in such cases as "ceil" and "floor". But I digress.

Your "string length" falls into this category. Raise a JIRA issue about it. But for now you you can use mapReduce and the existing JavaScript functionality:

db.collection.mapReduce(
    function() {
        emit( this.item.length, this.item );
    },
    function(key,values) {
        return values;
    },
    { "out": { "inline": 1 } }
)

So while that does actually have the "mapReduce" funky style of returning a re-shaped document and with of course everything matching the same length in an array, what it does do is take advantage of the nature of "mapReduce" ( not just restricted to MongoDB ) and allows the emitted "key" value to be sorted in the response.

like image 171
Neil Lunn Avatar answered Oct 20 '22 10:10

Neil Lunn


There is now a solution for this in MongoDB v3.4+ using the aggregation framework using $strLenBytes. Given the following document:

{_id: 0, name: "Bob"}

We can use

db.mycollection.aggregate([{
    $project: {
         byteLength: {$strLenBytes: "$name"}
    }
}])

Which will return 3 for the number of bytes.

like image 6
rrrr-o Avatar answered Oct 20 '22 09:10

rrrr-o