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 :)
The field that has the numbers you want to sort is the
sq_itemgrupodados
?– Tuxpilgrim
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.
– Gerci Lisboa