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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:58:00+00:00 2026-06-07T16:58:00+00:00

My data set consists of documents containing a field with an array of integers.

  • 0

My data set consists of documents containing a field with an array of integers. When i’m counting on objects whose field contains elements from some range it seems that index scan performance decreases with higher values indexBounds (but the same amount of values scanned by the range).

Test data:

for (var i = 0; i < 100000; i++) db.foo.insert({tts:(function(){var val = [];for(var j = 0; j < 100; j++) {val[j] = j} return val;})()});
db.foo.ensureIndex({tts:1});

Queries:

> db.foo.find({tts:{$elemMatch:{$gte:10, $lte:10}}}).explain()
{
    "cursor" : "BtreeCursor tts_1",
    "isMultiKey" : true,
    "n" : 100000,
    "nscannedObjects" : 100000,
    "nscanned" : 100000,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 1,
    "nChunkSkips" : 0,
    "millis" : 313,
    "indexBounds" : {
        "tts" : [
            [
                10,
                10
            ]
        ]
    },
    "server" : "localhost:27017"
}
> db.foo.find({tts:{$elemMatch:{$gte:90, $lte:90}}}).explain()
{
    "cursor" : "BtreeCursor tts_1",
    "isMultiKey" : true,
    "n" : 100000,
    "nscannedObjects" : 100000,
    "nscanned" : 100000,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 1,
    "nChunkSkips" : 0,
    "millis" : 1286,
    "indexBounds" : {
        "tts" : [
            [
                90,
                90
            ]
        ]
    },
    "server" : "localhost:27017"
}

In fact I have near 200 values in this field and query gets up to 10 times slower when the requested range have the highest boundaries. (Each value in the field belongs to a unique range, all ranges select the same amount of objects (100000), querying is performed only for subranges of this ranges)

Collection stats:

> db.foo.stats()
{
    "ns" : "test.foo",
    "count" : 100000,
    "size" : 122400128,
    "avgObjSize" : 1224.00128,
    "storageSize" : 140763136,
    "numExtents" : 12,
    "nindexes" : 2,
    "lastExtentSize" : 40071168,
    "paddingFactor" : 1,
    "systemFlags" : 1,
    "userFlags" : 0,
    "totalIndexSize" : 254845920,
    "indexSizes" : {
        "_id_" : 3262224,
        "tts_1" : 251583696
    },
    "ok" : 1
}

Is there a workaround for this problem?

Thanks.

  • 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-07T16:58:01+00:00Added an answer on June 7, 2026 at 4:58 pm

    Mongo is able to use the index to determine that there is an element in each of the documents that matches the $lte and $gte conditions. $elemmatch requires that a single element match both conditions so mongo scans each of the documents (and the subarray) to determine whether such an element exists. For the larger values, mongo has to scan 90 elements into each array instead of just the first 10 to find a matching element. Thus a query the matches elements towards the end of a long array will take longer.

    Note that if you reverse the array, the behavior is reversed:

    for (var i = 0; i < 100000; i++) db.foo.insert({tts:(function(){var val = [];for(var j = 100; j >= 0; j--) {val[j] = j} return val;})()});
    

    It looks like this might be related to https://jira.mongodb.org/browse/SERVER-6002. Using the latest development release might fix the problem at the cost of stability.

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

Sidebar

Related Questions

I have the following data set with the 3rd field consists of 0's and
I have a C++ application whose in-memory data set consists of a set of
I have a data set which consists of an ID and a matrix (n
My data set contains a column named IconUri that contains a full HTTP address
I have a set of data that consists of a filenbr, open date and
I have some data files from a legacy system that I would like to
I've been given some strings to work with. Each one represents a data set
I have an array of hashes that consists of my product data in @product_records.
I have a class that performs some operations on a set of data. The
My test project consists of a set of input data files which is fed

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.