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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:10:25+00:00 2026-06-10T03:10:25+00:00

I was reading the documentation for Mongo Shard keys for 2.2, and found it

  • 0

I was reading the documentation for Mongo Shard keys for 2.2, and found it a bit confusing.

All sharded collections must have an index that starts with the shard
key.
If you shard a collection that does not yet contain documents and
without such an index, the shardCollection will create an index on the
shard key. If the collection already contains documents, you must
create an appropriate index before using shardCollection.

Changed in version 2.2: The index on the shard key no longer needs to
be identical to the shard key
. This index can be an index of the shard
key itself as before, or a compound index where the shard key is the
prefix of the index.
This index cannot be a multikey index.

If you have a collection named people, sharded using the field {
zipcode: 1 }, and you want to replace this with an index on the field
{ zipcode: 1, username: 1 }, then:

Create an index on { zipcode: 1, username: 1 }: db.people.ensureIndex(
{ zipcode: 1, username: 1 } ); When MongoDB finishes building the
index, you can safely drop existing index on { zipcode: 1 }:
db.people.dropIndex( { zipcode: 1 } ); Warning The index on the shard
key cannot be a multikey index.
As above, an index on { zipcode: 1,
username: 1 } can only replace an index on zipcode if there are no
array values for the username field.

If you drop the last appropriate index for the shard key, recover by
recreating a index on just the shard key.

I have a couple of questions about shard keys and indexes.

i) From the documentation, it looks like multi-key indexes were supported before 2.2. If that is the case, how is a compound index different from multikey indexes ?

ii) What is the difference between having
[a] an index that starts with a shard key and
[b] an index which has a shard key as a prefix ?

iii) What is the warning note about an index on the shard key should not be a multikey index ?
Isn’t db.people.ensureIndex( { zipcode: 1, username: 1 } a multikey index ?

  • 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-10T03:10:26+00:00Added an answer on June 10, 2026 at 3:10 am

    How is a compound index different from a multikey index?:

    A compound index is an index like the one you described in the example:

    { zipcode: 1, username: 1 }

    A multikey index is one that indexes items in an array, like an index on tags that is used to return all documents that contain the tag ‘mongoDB’,

    What is the difference between having [a] an index that starts with a shard key and [b] an index which has a shard key as a prefix?:

    Nothing.

    What is the warning note about an index on the shard key should not be a multikey index?:

    This makes a fair bit of sense when you consider that a multikey index is an index on an array. Consider our index on a tags array. A document could easily live in many (or all) shards, if it had the right collection of values in the array.

    In other words, documents still have to be sharded based on a single value, as opposed to an object or an array.

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

Sidebar

Related Questions

While reading documentation on some library, I saw that some library have some feature,
I have been reading documentation about the new features that IIS 7.5 offers regarding
Reading Apple documentation i found that UIEventTypeMotion is useful only to intercept Shake. It
Reading through documentation, I found following: 1.9.1 1.8.4 1.8.2 A version of 1.8.2 select
Reading the documentation it seems this might not be possible, but it seems that
When reading some documentation about assertions, I found: java -ea -dsa Enables assertions in
I'm reading this documentation article about Custom Annotations in Doctrine . I understand that
I'm reading the documentation now, and I have 1 thing to be fixed -
I am reading documentation that says setValue:forKey: will treat a NULL value as if
I have been reading documentation describing class inheritance, abstract base classes and even python

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.