Mongodb sort by number Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!MongoDB vs. CassandraHow to query MongoDB with “like”?Update MongoDB field using value of another fieldMongoDB relationships: embed or reference?How do I drop a MongoDB database from the command line?Using .sort with PyMongoHow does MongoDB sort records when no sort order is specified?“Large data” work flows using pandasmaximum limit for MongoDB 2.6 sort querySorting on index of array mongodb
Is there any word for a place full of confusion?
Should I follow up with an employee I believe overracted to a mistake I made?
Why does the remaining Rebel fleet at the end of Rogue One seem dramatically larger than the one in A New Hope?
Central Vacuuming: Is it worth it, and how does it compare to normal vacuuming?
How could we fake a moon landing now?
Illegal assignment from sObject to Id
What does it mean that physics no longer uses mechanical models to describe phenomena?
The code below, is it ill-formed NDR or is it well formed?
Do I really need to have a message in a novel to appeal to readers?
Hangman Game with C++
What was the first language to use conditional keywords?
ArcGIS Pro Python arcpy.CreatePersonalGDB_management
How does light 'choose' between wave and particle behaviour?
How can I reduce the gap between left and right of cdot with a macro?
Can anything be seen from the center of the Boötes void? How dark would it be?
Drawing without replacement: why is the order of draw irrelevant?
Why wasn't DOSKEY integrated with COMMAND.COM?
Why does it sometimes sound good to play a grace note as a lead in to a note in a melody?
Can a new player join a group only when a new campaign starts?
Chebyshev inequality in terms of RMS
What do you call the main part of a joke?
Maximum summed subsequences with non-adjacent items
Disembodied hand growing fangs
Why should I vote and accept answers?
Mongodb sort by number
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!MongoDB vs. CassandraHow to query MongoDB with “like”?Update MongoDB field using value of another fieldMongoDB relationships: embed or reference?How do I drop a MongoDB database from the command line?Using .sort with PyMongoHow does MongoDB sort records when no sort order is specified?“Large data” work flows using pandasmaximum limit for MongoDB 2.6 sort querySorting on index of array mongodb
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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?
mongodb
add a comment |
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?
mongodb
add a comment |
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?
mongodb
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?
mongodb
mongodb
asked Jun 10 '14 at 19:12
dzmdzm
9,44336107196
9,44336107196
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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"
add a comment |
This can help You to sort in ascending order :-db.collectionName.find().sort(sortingParameter: 1).collation(locale: "en_US", numericOrdering: true)
:)
add a comment |
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") );;
);
1
Welcome to stackoverflow. In addition to the answer you've provided, please consider providing a brief explanation of why and how this fixes the issue.
– jtate
Mar 8 at 22:09
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24149105%2fmongodb-sort-by-number%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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"
add a comment |
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"
add a comment |
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"
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"
answered Jun 10 '14 at 19:34
Christian PChristian P
9,28544864
9,28544864
add a comment |
add a comment |
This can help You to sort in ascending order :-db.collectionName.find().sort(sortingParameter: 1).collation(locale: "en_US", numericOrdering: true)
:)
add a comment |
This can help You to sort in ascending order :-db.collectionName.find().sort(sortingParameter: 1).collation(locale: "en_US", numericOrdering: true)
:)
add a comment |
This can help You to sort in ascending order :-db.collectionName.find().sort(sortingParameter: 1).collation(locale: "en_US", numericOrdering: true)
:)
This can help You to sort in ascending order :-db.collectionName.find().sort(sortingParameter: 1).collation(locale: "en_US", numericOrdering: true)
:)
answered Feb 23 at 11:27
DeveshDevesh
394
394
add a comment |
add a comment |
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") );;
);
1
Welcome to stackoverflow. In addition to the answer you've provided, please consider providing a brief explanation of why and how this fixes the issue.
– jtate
Mar 8 at 22:09
add a comment |
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") );;
);
1
Welcome to stackoverflow. In addition to the answer you've provided, please consider providing a brief explanation of why and how this fixes the issue.
– jtate
Mar 8 at 22:09
add a comment |
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") );;
);
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") );;
);
edited Mar 8 at 20:30
pzaenger
3,30011727
3,30011727
answered Mar 8 at 20:29
Vivek YadavVivek Yadav
11
11
1
Welcome to stackoverflow. In addition to the answer you've provided, please consider providing a brief explanation of why and how this fixes the issue.
– jtate
Mar 8 at 22:09
add a comment |
1
Welcome to stackoverflow. In addition to the answer you've provided, please consider providing a brief explanation of why and how this fixes the issue.
– jtate
Mar 8 at 22:09
1
1
Welcome to stackoverflow. In addition to the answer you've provided, please consider providing a brief explanation of why and how this fixes the issue.
– jtate
Mar 8 at 22:09
Welcome to stackoverflow. In addition to the answer you've provided, please consider providing a brief explanation of why and how this fixes the issue.
– jtate
Mar 8 at 22:09
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24149105%2fmongodb-sort-by-number%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown