1

I have a field in MongoDB that is a String type. It will contain some combination of letters and numbers like "10", "101", "11", "112", "x115", "abc.5". Right now if I tell MongoDB to sort these it will do an alphabetical sort, ordering as so:

  • 10
  • 101
  • 11
  • 112
  • abc.5
  • x115

It orders "101" before "11", how could I change the sorting so that numerals are ordered properly?

grivescorbett
  • 1,605
  • 1
  • 21
  • 29
  • You'd need to zero pad the numbers to get them to sort consistently when stored as a string (and then remove the zeros later if needed). – WiredPrairie Feb 06 '13 at 13:09

2 Answers2

2

You might want to use db.eval if you are determined to do this on the database-side.

Answer extracted from another question:

I don't think this is possible directly; the sort documentation certainly doesn't mention any way to provide a custom compare function.

You're probably best off doing the sort in the client, but if you're really determined to do it on the server you might be able to use db.eval() to arrange to run the sort on the server (if your client supports it).

Server-side sort:

db.eval(function() { 
  return db.scratch.find().toArray().sort(function(doc1, doc2) { 
    return doc1.a - doc2.a 
  }) 
});

Versus the equivalent client-side sort:

db.scratch.find().toArray().sort(function(doc1, doc2) { 
  return doc1.a - doc2.b 
});
Community
  • 1
  • 1
bevacqua
  • 47,502
  • 56
  • 171
  • 285
  • 1
    Given that eval blocks all reads and writes, `eval` isn't a good production-system option. – WiredPrairie Feb 06 '13 at 02:51
  • 1
    Here are the full set of warnings for it: http://docs.mongodb.org/manual/reference/method/db.eval/. The summary is to not use it except in exceptional situations. – WiredPrairie Feb 06 '13 at 11:40
  • I linked to that in my post, most of them revolve around the exclusive lock not being disabled – bevacqua Feb 06 '13 at 13:37
  • It blocks all JavaScript & can't be used in a sharded system. That could be an issue. – WiredPrairie Feb 06 '13 at 13:46
  • You **can disable that lock behavior** using **`db.runCommand()`** rather than the helper, you can disable the lock by setting the `nolock` flag to `true` if the eval performs a strictly read-only operation. and this **is** a strictly read-only operation. – bevacqua Feb 06 '13 at 13:48
2

You can use collation with the numericOrdering parameter. Example:

db.collectionName.find()
    .sort({fieldToSortOnName: -1})
    .collation({locale: "en_US", numericOrdering: true})

Here locale: en_US is used for making a case insensitive search or sort.

svarog
  • 9,477
  • 4
  • 61
  • 77
AKSHAY JAIN
  • 236
  • 1
  • 6