Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9252055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:51:02+00:00 2026-06-18T10:51:02+00:00

I have a field in MongoDB that is a String type. It will contain

  • 0

I have a field in MongoDB that is a String type. It will contain some combination of letters and numbers like “10”, “101”, “11”, “112”, “x115”, “abc.5”. Right now if I tell MongoDB to sort these it will do an alphabetical sort, ordering as so:

  • 10
  • 101
  • 11
  • 112
  • abc.5
  • x115

It orders “101” before “11”, how could I change the sorting so that numerals are ordered properly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:51:04+00:00Added an answer on June 18, 2026 at 10:51 am

    You might want to use db.eval if you are determined to do this on the database-side.

    Answer extracted from another question:

    I don’t think this is possible directly; the sort
    documentation

    certainly doesn’t mention any way to provide a custom compare
    function.

    You’re probably best off doing the sort in the client, but if you’re
    really determined to do it on the server you might be able to use
    db.eval() to arrange to run the sort on the server (if your client
    supports it).

    Server-side sort:

    db.eval(function() { 
      return db.scratch.find().toArray().sort(function(doc1, doc2) { 
        return doc1.a - doc2.a 
      }) 
    });
    

    Versus the equivalent client-side sort:

    db.scratch.find().toArray().sort(function(doc1, doc2) { 
      return doc1.a - doc2.b 
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a field in mongodb that's a string: {"field": "some text"} . I
In MongoDB, I have a document with a field called ClockInTime that was imported
I have a field in a MongoDB document that stores an arbitrarily-large number. When
i have a rails controller grabbing some data from mongodb. the field i'm interested
I have a mongoDB collection with an array field that represents the lists the
The situation I have is that I'm querying MongoDB with a string for a
I have field that it's not a key (it's text) and I want to
I have field like a_b_c_d I want as the output a b c_d, this
I have field X that contains text with spaces in the end of the
I have a field on my User model that is protected because it determines

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.