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

  • Home
  • SEARCH
  • 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 7076187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:16:05+00:00 2026-05-28T06:16:05+00:00

I have a collection where I only ever need to look up documents by

  • 0

I have a collection where I only ever need to look up documents by whole arrays; I can’t think of any scenario where I’d want to look up documents by just one value of that array. Unfortunately, the multikey feature that is always activated for array values apparently can’t be deactivated.

In the documentation it says “The index will be used to lookup a subset of the values (currently the first one) and then the document will be inspected for the exact match.” I think this greatly decreases the performance in my case. Despite the index some lookups take 70 ms and some several minutes, because, depending on the first element, MongoDB sometimes has to search through a few thousand or several hundred thousand documents. At least that is my theory.

Is there some way to avoid this problem, or should I rather serialize my arrays and store them as strings?

Thanks in advance!

  • 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-28T06:16:06+00:00Added an answer on May 28, 2026 at 6:16 am

    Perhaps you could use a subdocument like:

    {
      array_sub_doc: { arr: [1,2,3,4,5] }
    }
    

    So that you can do matches like:

    db.coll.ensureIndex({array_sub_doc:1});
    db.coll.find({array_sub_doc: {arr:[1,2,3,4,5]}})
    

    update I discovered what caused the failure for large arrays. Index keys > 800 bytes will not be indexed. So, if you have a large subdocument and you put an index on it, if it’s greater than 800 bytes, and you try to search for it, you won’t find it. If you take the index off and search again for the same subdocument, you’ll find it (although it will be a full collection scan).

    This is documented here as a limitation, and will be removed in future releases: https://jira.mongodb.org/browse/SERVER-3372

    So, this will work in general for small arrays.

    Here’s some test code in case anyone wants to try it out:

    var randomArray = function() {
      var len = 80;
      var randomarr = new Array();
      for (var i=0; i<len; i++) {
        randomarr.push(Math.floor(Math.random() *10000));
      }
      return randomarr;
    }
    
    var insert = function() {
      db.Test2.ensureIndex({array_sub_doc:1});
      for(var i=0;i<10000;i++) {
        db.Test2.save({array_sub_doc: {arr: randomArray()}});
      }
    }
    
    db.Test2.remove();
    insert();
    
    var one = db.Test2.findOne();
    db.Test2.findOne({array_sub_doc:one.array_sub_doc});
    
    //...
    
    db.Test2.find({array_sub_doc:one.array_sub_doc}).explain(0);
    /* outputs:
    {
      "cursor" : "BtreeCursor array_sub_doc_1",
      "nscanned" : 1,
      "nscannedObjects" : 1,
      ...
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a collection of the form: map<key, list<object> > I only ever insert
I have a need for a fairly specialised collection .NET, and I don't think
I have an ICollection that I know will only ever have one member. Currently,
I have a collection of objects to which I'd like to just add a
I have a collection of classes that I want to serialize out to an
I have a collection of elements that I need to operate over, calling member
I have a class that is only ever accessed externally through static methods. Those
Have you ever looked for a web API for certain data or functionality, only
I have Collection List<Car> . How to compare each item from this collection with
I'm planning to have collection of items stored in a TCollection. Each item will

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.