sort numeric data mongodb

0

I'm trying to sort numeric data with a field set to number in the collection and sorting does not show as I need it. Example: 1,2,3,10 after ordering the result presented: 1,10,2,3. How to modify the result? I'm using the command:

var itgrpdados = (dados.grpdados).sort({sq_itemgrupodados:1});
    
asked by anonymous 25.07.2018 / 17:23

2 answers

1

I tried to set up a scenario as close as possible to yours, and I got some interesting results.

1. I created a database named dados

2. I created a collection named grpdados

And I've added 4 documents in collection grpdados :

{
    "_id":  {
    "$oid": "5b5923b4fb6fc07c4c24156e"
},
    "sq_itemgrupodados": "1"
},
{
    "_id": {
        "$oid": "5b592417fb6fc07c4c241593"
    },
    "sq_itemgrupodados": "2"
},
{
    "_id": {
        "$oid": "5b5923fcfb6fc07c4c24158e"
    },
    "sq_itemgrupodados": "3"
},{
    "_id": {
        "$oid": "5b592429fb6fc07c4c241595"
    },
    "sq_itemgrupodados": "10"
}

After that, I searched for sq_itemgrupodados and ordered using sort() upwards:

dados.grpdados.sort({"sq_itemgrupodados":1})

Result:

{1, 10, 2, 3} Exactly like yours, all right up to here!

So I made a small change to the documents, instead of passing string , I changed it to the value inteiro , example:

 {
        "_id":  {
        "$oid": "5b5923b4fb6fc07c4c24156e"
    },
        "sq_itemgrupodados": 1
 }

Doing this for the 4 documents, and doing the search again and sorting with sort() :

Result:

{1,2,3,10} ( sorted in ascending order as we wanted! )

Conclusion

I think it was just a misconception about the type of data you are trying to sort, for string it will sort by the value of the table ascii , logo 10 < 2 and 10 < 3 .

EDIT: I created a sandbox for testing on mongolab , so if that does not solve we can try other alternatives:     

26.07.2018 / 04:25
-1

@Tuxpilgrim I ran a test, but it did not work. Maybe because I'm performing sort after completing the Find command, because what I want to order is a sub form. Home where the data item is the collection, grpdados subform of data and sq_itemgrupodados is where are the data to sort. I read the form using the command:

Grupodadositens.findById(req.params.id,function(err,dados){
-...
var itgrpdados = (dados.grpdados).sort({sq_itemgrupodados:1});
});

In the definition of the collection the item sq_itemgrupodados is Number. follows the collection definition:

var mongoose = require('mongoose');

module.exports = function() {
    var itensgrpdadosSchema = mongoose.Schema({
        ds_itemgrupodados   : {type: String, trim: true},
        sq_itemgrupodados   : {type: Number, trim: true,default:0},
        id_exb_grp_dados    : {type: String, trim: true}
    });
    var grupodadosSchema = mongoose.Schema({
        cd_grupodados   : {type: String, trim: true, index: true},
        ds_grupodados   : {type: String, required: true,trim: true},
        cd_entidade     : {type: String, trim: true},
        ds_objgrpdados  : {type: String, trim: true},
        ds_titulogrupo  : {type: String, required: true,trim: true},
        grpdados        : [itensgrpdadosSchema],
        id_ativo        : {type: String, trim: true},
        data_cad        : {type: Date, default: Date.now}
    });
    return mongoose.model('Grupodados', grupodadosSchema);
}

Question: Would you like to order directly in the initial reading, without first reading the form and then ordering as I did above?

    
02.08.2018 / 15:38