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 7191549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:45:11+00:00 2026-05-28T19:45:11+00:00

In MongoDB, a field can have multiple values (an array of values). Each of

  • 0

In MongoDB, a field can have multiple values (an array of values). Each of them is indexed, so you can filter on any of the values. But can you also “order by” a field with multiple values and what is the result?

Update:

> db.test.find().sort({a:1})
{ "_id" : ObjectId("4f27e36b5eaa9ebfda3c1c53"), "a" : [ 0 ] }
{ "_id" : ObjectId("4f27e3845eaa9ebfda3c1c54"), "a" : [ 0, 1 ] }
{ "_id" : ObjectId("4f27df6e5eaa9ebfda3c1c4c"), "a" : [ 1, 1, 1 ] }
{ "_id" : ObjectId("4f27df735eaa9ebfda3c1c4d"), "a" : [ 1, 1, 2 ] }
{ "_id" : ObjectId("4f27df795eaa9ebfda3c1c4e"), "a" : [ 2, 1, 2 ] }
{ "_id" : ObjectId("4f27df7f5eaa9ebfda3c1c4f"), "a" : [ 2, 2, 1 ] }
{ "_id" : ObjectId("4f27df845eaa9ebfda3c1c50"), "a" : [ 2, 1 ] }
{ "_id" : ObjectId("4f27e39a5eaa9ebfda3c1c55"), "a" : [ 2 ] }

With unequal length arrays the longer array is “lower” than the shorter array

So, why is [0] before [0,1], but [2] after [2,1] ?
Is maybe sorting only done on the first array element? Or the lowest one? And after that it is insertion order?

Also, how is this implemented in the case of an index scan (as opposed to a table scan)?

  • 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-05-28T19:45:12+00:00Added an answer on May 28, 2026 at 7:45 pm

    Sorting of array elements is pretty complicated. Since array elements are indexed seperately sorting on an array field will actually result in some interesting situations. What happens is that MongoDB will sort them based on the lowest or highest value in the array (depending on sort direction). Beyond that the order is natural.

    This leads to things like :

    > db.test.save({a:[1]})
    > db.test.save({a:[0,2]})
    > db.test.find().sort({a:1})
    { "_id" : ObjectId("4f29026f5b6b8b5fa49df1c3"), "a" : [ 0, 2 ] }
    { "_id" : ObjectId("4f2902695b6b8b5fa49df1c2"), "a" : [ 1 ] }
    > db.test.find().sort({a:-1})
    { "_id" : ObjectId("4f29026f5b6b8b5fa49df1c3"), "a" : [ 0, 2 ] }
    { "_id" : ObjectId("4f2902695b6b8b5fa49df1c2"), "a" : [ 1 ] }
    

    In other words. The same order for reversed sorts. This is due to the fact that the “a” field of the top document holds both the lowest and the highest value.

    So effectively for the sort MongoDB ignores all values in the array that are not either the highest ({field:-1} sort) or the lowest ({field:1} sort) and orders the remaining values.

    To paint an (oversimplified) picture it works something like this :

    flattened b-tree for index {a:1} given above sample docs :

    "a" value 0 -> document 4f29026f5b6b8b5fa49df1c3
    "a" value 1 -> document 4f2902695b6b8b5fa49df1c2
    "a" value 2 -> document 4f29026f5b6b8b5fa49df1c3
    

    As you can see scanning from both top to bottom and bottom to top will result in the same order.

    Empty arrays are the “lowest” possible array value and thus will appear at the top and bottom of the above queries respectively.

    Indexes do not change the behaviour of sorting on arrays.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using MongoDB... If I have an array of ids in one field, can I
I'm working at a SaaS, where any tenant can have several lists of contacts,
When I retrieve Document from MongoDB, I want to filter out the id field
I have several documents in a MongoDB Collection, with a field 'name' (which is
I have a little problem, hope You can help me. I have next array
I have some rails application with MongoDB, Mongoid mapper and devise. Аuthorized user can
I have a field in mongodb that's a string: {"field": "some text"} . I
I have the following MongoDB object stored in my Mongo database: array ( '_id'
I have launch mongodb server: [demas@arch.local.net][~]% mongod --dbpatmongod --dbpath /home/demas/temp/ Mon Apr 19 09:44:18
How can I set up MongoDB so it can run as a Windows service?

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.