dzm
dzm

Reputation: 23574

Mongodb sort by number

I'm trying to sort by a field that includes numbers and when doing .sort({title: 1}) it will return results like 1,10,11,12,2,3,4. I would like to have it return like 1,2,3,4,10,11,12, is this possible directly from mongo?

Upvotes: 3

Views: 13388

Answers (4)

Devesh
Devesh

Reputation: 959

Example:

db.test.find().sort({a:1})
{ "_id": ObjectId("60a018168823ff3bd585b308"), "a": "1" }
{ "_id": ObjectId("60a0181d8823ff3bd585b30a"), "a": "10" }
{ "_id": ObjectId("60a0181f8823ff3bd585b30b"), "a": "11" }
{ "_id": ObjectId("60a018198823ff3bd585b309"), "a": "2" }

with collation and numericOrdering

db.test.find().sort({a:1}).collation({ locale: "en_US", numericOrdering: true }).ugly()
{ "_id": ObjectId("60a018168823ff3bd585b308"), "a": "1" }
{ "_id": ObjectId("60a018198823ff3bd585b309"), "a": "2" }
{ "_id": ObjectId("60a0181d8823ff3bd585b30a"), "a": "10" }
{ "_id": ObjectId("60a0181f8823ff3bd585b30b"), "a": "11" }

This answer can help in sort ascending order

db.collectionName.find()
                 .sort({sortingParameter: 1})
                 .collation({
                     locale: "en_US",
                     numericOrdering: true
                 })
from pymongo import MongoClient
from pymongo.collation import Collation

collectionName = MongoClient().testDB.collectionName

collectionName.find()
                 .sort('sortingParameter')
                 .collation(Collation(locale='en_US', numericOrdering=True))

Upvotes: 4

mckenzm
mckenzm

Reputation: 1840

(Rather than the field actually being a string)

It sounds like you are (or were) sorting an array, not a cursor.

If you are using a shell interface, remember sort() treats numbers in an array as left justified strings.

db.foo.distinct("bar").sort(function(x,y) {return x - y})

to reverse, return (y - x) instead.

You could pre-define the function:

compareNumeric = function(x,y) {return x - y}
db.foo.distinct("bar").sort(compareNumeric)

Apologies for the answer being a little late, but the nature of the list in the question being single values of one type rather than interleaved with other BSON properties and fields bothered me. It really needed to be "dressed".

Upvotes: 0

Vivek Yadav
Vivek Yadav

Reputation: 39

router.get('/projectstatus/list', (req, res) => {    
    Addstatus
        .find()
        .sort({order: 1})
        .collation({locale: "en_US", numericOrdering: true})
        .then(addstatus => { res.json(addstatus) })
        .catch(err => { console.log("------------------Data is not found----------------\n") });;
});

Upvotes: 1

Christian P
Christian P

Reputation: 12240

You are probably sorting your documents on a field that contains strings, not numbers. MongoDB sorts numbers in their natural order and strings in their alphabetical order.

You will need to convert values of your title field to integers to get natural ordering.

This is an example result from sorting a collection on field a that contains strings and integers.

{ _id: ObjectId("53975b47bff015b5a400097b"), a: 1 }
{ _id: ObjectId("53975b74bff015c524000864"), a: 9 }
{ _id: ObjectId("53975b5d74c5e23516000919"), a: 10 }
{ _id: ObjectId("53975b5d74c5e23516000200"), a: "1" }
{ _id: ObjectId("53975b8ebff01545bd0006b4"), a: "10" }
{ _id: ObjectId("53975b9e32105bb92f0007fd"), a: "9" }

Upvotes: 3

Related Questions