Mongo sort by string value that is actually number

It seems to me that the best solution here would be to parse it first as an integer. You could do it using a simple script in javascript like this, using the mongodb client for node:

db.collection.find({}, {internalDate: 1}).forEach(function(doc) {
    db.collection.update(
       { _id: doc._id },
       { $set: { internalDate: parseInt(doc.internalDate) } }
    )
})

you also can use the aggregate method to sort number which is actually a string.

 db.collection.aggregate([{
     $sort : {
          internalDate : 1
     }
 }], {
     collation: {
         locale: "en_US",
         numericOrdering: true
     }
 });

if you are using mongoose-paginate package for serverside pagination .. so don't use this package, use only mongoose-paginate-v2 for serverside pagination. this package for nodejs side


I was having this issue. I use string length to sort first and then apply the sort of my numeric value stored like a string. e.g. "1", "100", "20", "3" that should be sorted like 1, 3, 29, 100.

    db.AllTours.aggregate([
    {
        $addFields : {
            "MyStringValueSize" : { $strLenCP: "$MyValue" }
        }
    },
    { 
        $sort : { 
            "MyStringValueSize" : 1,
            "MyValue" : 1
        } 
    }
    ]);

There is a new feature in version 4.0 called $toInt that can be used to parse your string and then sort. In my case I can't upgrade from 3.6.


Collation is what you need...

db.collection.find()
  .sort({internalDate: 1})
  .collation({locale: "en_US", numericOrdering: true})

Tags:

Mongodb