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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:58:03+00:00 2026-05-25T06:58:03+00:00

I’ve noticed that for indexing Arrays, the MongoDB documentation suggests that Multikeys are what

  • 0

I’ve noticed that for indexing Arrays, the MongoDB documentation suggests that Multikeys are what should be used. However it doesn’t state anything about explicitly creating multikeys as indexes. It appears as though this is automatic. Is that correct? Do I not need to created indexes on array elements? Should I create an index on the parent field?

http://www.mongodb.org/display/DOCS/Indexes#Indexes-IndexingArrayElements
http://www.mongodb.org/display/DOCS/Multikeys

Update

I should have added that the arrays I’m speaking of contain embedded documents. Here’s a specific example of part of a document:

{
 _a: [{
    _aid: "4e5d43698d3a329553111227",
    _an:  true
    _aim: "http://graph.facebook.com/7403766/picture"
    _ana: "Nick ONeill"
      },
       {
    _aid: "4e5d43698d3a329553111228",
    _an:  true
    _aim: "http://graph.facebook.com/129312401/picture"
    _ana: "Joe Peterson"
      }]
 }
  • 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-25T06:58:04+00:00Added an answer on May 25, 2026 at 6:58 am

    An index on an array field, or one containing an array field, is automatically a multikey index. What this means is that in the index there will be multiple entries, one for each element of the array, and one for the array as a whole. This allows matching on individual array elements like:

    db.foo.find({myArrayField: "bar"})
    

    Which will match documents like:

    {name: "whatever", myArrayField: ["foo", "bar", "baz"]}
    

    When querying such a field, MongoDB ensures that any given document is only returned once, even if it matches multiple times:

    > db.foo.find({myArrayField: {$in: ["foo", "bar"]}})
    {"name" : "whatever", "myArrayField" : [ "foo", "bar", "baz" ]}
    

    Edit: You can index an array which contains embedded documents as well as “primitive” types. When doing so, you might want to use the $elemMatch operator to search for partial matches on the embedded documents:

    (Note about edit: I’ve updated the example to demonstrate using $elemMatch with multiple elements; when the query only uses one embedded document field, you do not need $elemMatch)

    > db.foo.save({name:"a", myArrayField: [{a:1, b:2}, {a:5, b:6}]})
    > db.foo.save({name:"b", myArrayField: [{a:1, b:3}]})
    > db.foo.save({name:"c", myArrayField: [{a:1, b:1}, {a:5, b:3}]})
    > db.foo.find({myArrayField: {a: 1, b: 2}})
    {name:"a", myArrayField: [{a:1, b:2}, {a:5, b:6}]}
    > db.foo.find({myArrayField: {a: 1}})
    // finds nothing, no document has a
    // sub-document in myArrayField
    // exactly equal to {a: 1}
    > db.foo.find({"myArrayField.a": 1})
    {name:"a", myArrayField: [{a:1, b:2}, {a:5, b:6}]}
    {name:"b", myArrayField: [{a:1, b:3}]}
    {name:"c", myArrayField: [{a:1, b:1}, {a:5, b:3}]}
    > db.foo.find({myArrayFIeld: {$elemMatch: {a: 1, b: {$gt: 1}}}})
    {name:"a", myArrayField: [{a:1, b:2}, {a:5, b:6}]}
    {name:"b", myArrayField: [{a:1, b:3}]}
    // this does not find document "b", since
    // it does not have any sub-documents in
    // myArrayField where a is 1 and b is
    // greater than 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is

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.