sort numerical data mongodb

Asked

Viewed 708 times

0

I am trying to sort numeric data with a field set as number in Collection and the sort does not present as I need. Example: 1,2,3,10 after sorting the displayed result: 1,10,2,3. How to modify the result? I’m using the command:

var itgrpdados = (dados.grpdados).sort({sq_itemgrupodados:1});
  • The field that has the numbers you want to sort is the sq_itemgrupodados?

  • yes, this field is to sort the data for the user to define which sequence: it is set in Collection as Number, but I have tried as String and did not change the result.

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 called dados

2. I created a Collection calling for grpdados

And 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 did a search for sq_itemgrupodados and ordered using the sort() ascendantly:

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

Upshot:

{1, 10, 2, 3} (Just like yours, all right so far!)

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

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

Doing it for the 4 documents, and again searching and ordering with the sort():

Upshot:

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

Completion

I believe it was just a misunderstanding regarding the type of data you’re trying to sort, to string it will sort by the table value ascii, soon 10 < 2 and 10 < 3.

EDIT: I created a sandbox for testing in mongolab, so if that doesn’t work out we can try other alternatives :)

  • thanks for the help. Just to align I’m using nodejs, javascript. the item given was the result of a find: (I don’t know if it will be long) but I’ll try.

  • I tried to send a text but it got long, anyway I will try to carry out as suggested.

  • see collection definition: module.Exports = Function() { var itensgrpdadosSchema = Mongoose.Schema({ ds_itemgrupodate : {type: String, Trim: true}, sq_itemgroups : {type: Number, Trim: true,default:0}, }); var groupSchema = Mongoose.Schema({ cd_grouped : {type: String, Trim: true, index: true}, grpdados : [itensgrpdadosSchema] }); Return Mongoose.model('Grouped', groupSchema); }

  • the definition of the item sq_itemgrupodate is Number, and I am reading in js with the command:Groupall items.findById(req.params.id,Function(err,data){... var itgrpdados = (data.grpdados). Sort({sq_itemgrupoded:1}); .. }); ode the name of Collection is Groupofitems. I am trying to sort the item sq_itemgrupodados that is inside a sub with the grpdados name. (I don’t know if the formatting will give a good understanding).

  • @Gercilisboa, it would be interesting if you added this information to the body of the question. I’ll run some tests on the scenario you described and give you a feedback :)

  • 1

    OK, I am new to this forum, I will proceed this way from now on.

  • @Gercilisboa some result?

  • I edited the question again and includes more details, I do not know if this way will be more understandable. Grateful.

Show 3 more comments

-1

@Tuxpilgrim I performed a test, but it didn’t work. Maybe because I’m performing Sort after performing the Find command, because what I want order is a subform.
where the given item is Collection, grpdados data subform and sq_itemgrupodate is where the data is 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 Collection definition the item sq_itemgrupodate is Number. follows the definition of Collection:

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: Could you sort directly in the initial reading, without first reading the form and then sort as I did above?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.