-
Notifications
You must be signed in to change notification settings - Fork 1.7k
DOCS-1197 document hashed shard keys #757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,9 +37,9 @@ MongoDB indexes have the following core features: | |
requirements as you create indexes in your MongoDB environment. | ||
|
||
- All MongoDB indexes use a B-tree data structure. MongoDB can use | ||
these representation of the data to optimize query responses. | ||
this representation of the data to optimize query responses. | ||
|
||
- Every query, including update operations, use one and only one | ||
- Every query, including update operations, uses one and only one | ||
index. The :ref:`query optimizer <read-operations-query-optimization>` | ||
selects the index empirically by | ||
occasionally running alternate query plans and by selecting the plan | ||
|
@@ -82,7 +82,7 @@ Index Types | |
This section enumerates the types of indexes available in MongoDB. | ||
For all collections, MongoDB creates the default :ref:`_id index | ||
<index-type-id>`. You can create additional indexes with the | ||
:method:`ensureIndex() <db.collection.ensureIndex()>` method on any | ||
:method:`~db.collection.ensureIndex()` method on any | ||
single field or :ref:`sequence of fields <index-type-compound>` within | ||
any document or :ref:`sub-document <index-sub-document>`. MongoDB also | ||
supports indexes of arrays, called :ref:`multi-key indexes | ||
|
@@ -274,6 +274,13 @@ index, however, would not support queries that select the following: | |
- only the ``location`` and ``stock`` fields | ||
- only the ``item`` and ``stock`` fields | ||
|
||
|
||
.. note:: | ||
|
||
:ref:`Hashed indexes <index-hashed-index>` are incompatible with compound indexes. You will receive | ||
an error if you attempt to create a compound index including a hashed | ||
index. | ||
|
||
When creating an index, the number associated with a key specifies the | ||
direction of the index. The options are ``1`` (ascending) and ``-1`` | ||
(descending). Direction doesn't matter for single key indexes or for | ||
|
@@ -360,6 +367,14 @@ value in the array separately, in a "multikey index." | |
Queries could use the multikey index to return queries for any of | ||
the above values. | ||
|
||
.. note:: | ||
|
||
MongoDB computes values for a hashed index on the entire content of a field, | ||
including fields that hold arrays or sub-documents. | ||
|
||
For fields that hold arrays and sub-documents, you cannot use the index to | ||
support any query that introspects the value of an array or sub-document. | ||
|
||
You can use multikey indexes to index fields within objects embedded in | ||
arrays, as in the following example: | ||
|
||
|
@@ -509,6 +524,42 @@ By default, ``sparse`` is ``false`` on MongoDB indexes. | |
have the indexed field *are* indexed in a sparse index, even if | ||
that field stores a null value in some documents. | ||
|
||
.. index:: index; hashed | ||
.. _index-type-hashed: | ||
|
||
Hashed Index | ||
~~~~~~~~~~~~ | ||
|
||
.. versionadded:: 2.4 | ||
|
||
Hashed indexes contain entries consisting of a hash of the indexed field. | ||
Hashed indexes cannot be compound indexes. | ||
Hashed indexes can be created on only one field which may not contain | ||
an array as a value. | ||
Hashed indexes cannot have a ``unique`` constraint. | ||
|
||
MongoDB can use the hashed index to support equality queries, but cannot | ||
use these indexes for range queries. | ||
|
||
It is possible to create a hashed and non-hashed index on the same field: | ||
MongoDB will use the scalar index for range queries. | ||
|
||
.. _hashed-index-warning: | ||
|
||
.. include:: /includes/warning-hashed-index-floating-point.rst | ||
|
||
Create a hashed index using an operation that resembles the | ||
following: | ||
|
||
.. code-block:: javascript | ||
|
||
db.active.ensureIndex( { a: "hashed" } ) | ||
|
||
This operation creates a hashed index for the ``active`` collection on | ||
the ``a`` field. | ||
|
||
.. [#hash-size] The hash stored in the hashed index is 64 bits. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The hash is 64 bits, but we only store the first 32 bits? This assertion misleads given the truncation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The md5 hash is 128 bits, we store 64. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only notable if you know that MD5s are 128 bits, so maybe we should say that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes with the caveat that the hash function may change to sha1 or something else. |
||
|
||
.. index:: index; options | ||
.. _index-creation-operations: | ||
.. _index-operations: | ||
|
@@ -563,7 +614,7 @@ construction: | |
.. versionchanged:: 2.4 | ||
Before 2.4, a :program:`mongod` instance could only build one | ||
background index per database at a time. | ||
|
||
.. versionchanged:: 2.2 | ||
Before 2.2, a single :program:`mongod` instance could only build | ||
one index at a time. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.. warning:: | ||
|
||
There is no supported means to un-shard a collection after running | ||
:dbcommand:`shardCollection`. | ||
Additionally, once you have sharded a collection you cannot | ||
change shard keys, or update the value of any field used in | ||
your shard key index. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.. warning:: | ||
|
||
Hashed indexes truncate floating point numbers to 64-bit integers | ||
before hashing. For example, a hashed index would store the same | ||
value for a field that held a value of ``2.3``, ``2.2`` and ``2.9``. | ||
To prevent collisions do not use a hashed index for floating point | ||
numbers that cannot be consistently converted to 64-bit integers (and | ||
then back to floating point.) Hashed indexes do not support floating | ||
point values larger than 2\ :sup:`53`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,12 +14,16 @@ db.collection.ensureIndex() | |
fields to index and order of the index. A | ||
``1`` specifies ascending and a ``-1`` | ||
specifies descending. | ||
A value of ``"hashed"`` can be used to create an | ||
index on hashed values of the specified field. | ||
Hashed indexes are primarily used to support | ||
:term:`hashed shard keys`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should remove this addition, and refactor the page to say "To create other kinds of indexes, see " and link people out to another index type page, because we'll end up being too redundant in this section. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer handling the refactoring as a separate ticket. |
||
|
||
:param document options: A :term:`document` that controls the creation | ||
of the index. This argument is optional. | ||
|
||
.. warning:: Index names, including their full namespace | ||
(i.e. ``database.collection``) can be no longer than 128 | ||
(i.e. ``database.collection``) cannot be longer than 128 | ||
characters. See the :method:`db.collection.getIndexes()` field | ||
":data:`~system.indexes.name`" for the names of existing indexes. | ||
|
||
|
@@ -33,7 +37,7 @@ db.collection.ensureIndex() | |
``[key]``. | ||
|
||
If the ``keys`` document specifies more than one field, than | ||
:method:`db.collection.ensureIndex()` creates a :term:`compound | ||
:method:`~db.collection.ensureIndex()` creates a :term:`compound | ||
index`. To specify a compound index use the following form: | ||
|
||
.. code-block:: javascript | ||
|
@@ -43,6 +47,8 @@ db.collection.ensureIndex() | |
This command creates a compound index on the ``key`` field | ||
(in ascending order) and ``key1`` field (in descending order.) | ||
|
||
A compound index cannot include a :ref:`hashed index <index-type-hashed>`. | ||
|
||
.. note:: | ||
|
||
The order of an index is important for supporting | ||
|
@@ -125,7 +131,7 @@ db.collection.ensureIndex() | |
and faster index format. | ||
|
||
Please be aware of the following behaviors of | ||
:method:`ensureIndex() <db.collection.ensureIndex()>`: | ||
:method:`~db.collection.ensureIndex()`: | ||
|
||
- To add or change index options you must drop the index using the | ||
:method:`db.collection.dropIndex()` and issue another | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't in the hashed index section which means it's probably a bit out of place, and also I thought re: our conversation yesterday, you couldn't use "hashed" index on multi-key fields.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You cannot create a hashed index on an array.
You can create a hashed index on apparently any other BSON type (so a: {b: {c: {d: 1, e: 2}}} is a valid document for a hashed index.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So "for fields that hold arrays" (which would be a multi-key index) is misleading or confusing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed references to arrays, which I thought I'd done already.