-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexes-hashed.txt
78 lines (47 loc) · 2.47 KB
/
indexes-hashed.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
==============
Hashed Indexes
==============
Learning Objectives
-------------------
Upon completing this module, students should understand:
- What a hashed index is
- When to use a hashed index
.. include:: /includes/student-notes.rst
What is a Hashed Index?
-----------------------
- Hashed indexes are based on field values like any other index.
- The difference is that the values are hashed and it is the hashed value that is indexed.
- The hashing function collapses sub-documents and computes the hash for the entire value.
- MongoDB can use the hashed index to support equality queries.
- Hashed indexes do not support multi-key indexes, i.e. indexes on array fields.
- Hashed indexes do not support range queries.
.. include:: /includes/student-notes.rst
Why Hashed Indexes?
-------------------
- In MongoDB, the primary use for hashed indexes is to support sharding a collection using a hashed shard key.
- In some cases, the field we would like to use to shard data would make it difficult to scale using sharding.
- Using a hashed shard key to shard a collection ensures an even distribution of data and overcomes this problem.
- See :manual:`Shard a Collection Using a Hashed Shard Key</tutorial/shard-collection-with-a-hashed-shard-key/>` for more details.
- We discuss sharding in detail in another module.
.. include:: /includes/student-notes.rst
Limitations
-----------
- You may not create compound indexes that have hashed index fields.
- You may not specify a unique constraint on a hashed index.
- You can create both a hashed index and a non-hashed index on the same field.
.. only:: instructor
.. note::
- For a field on which there is both a hashed index and a non-hashed index, MongoDB will use the non-hashed index for range queries.
.. include:: /includes/student-notes.rst
Floating Point Numbers
----------------------
- MongoDB hashed indexes truncate floating point numbers to 64-bit integers before hashing.
- Do not use a hashed index for floating point numbers that cannot be reliably converted to 64-bit integers.
- MongoDB hashed indexes do not support floating point values larger than 2\ :sup:`53`.
.. include:: /includes/student-notes.rst
Creating a Hashed Index
-----------------------
Create a hashed index using an operation that resembles the following. This operation creates a hashed index for the active collection on the ``a`` field.
.. code-block:: javascript
db.active.createIndex( { a: "hashed" } )
.. include:: /includes/student-notes.rst