/
avoid-text-index-name-limit.txt
93 lines (68 loc) · 2.21 KB
/
avoid-text-index-name-limit.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
===============================
Specify Name for ``text`` Index
===============================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
.. admonition:: Changed in MongoDB 4.2
:class: note
.. include:: /includes/extracts/4.2-changes-index-names.rst
The default name for the index consists of each indexed field name
concatenated with ``_text``. For example, the following command creates
a ``text`` index on the fields ``content``, ``users.comments``, and
``users.profiles``:
.. code-block:: javascript
db.collection.createIndex(
{
content: "text",
"users.comments": "text",
"users.profiles": "text"
}
)
The default name for the index is:
.. code-block:: javascript
"content_text_users.comments_text_users.profiles_text"
Specify a Name for ``text`` Index
---------------------------------
You can pass the ``name`` option to the
:method:`db.collection.createIndex()` method:
.. code-block:: javascript
db.collection.createIndex(
{
content: "text",
"users.comments": "text",
"users.profiles": "text"
},
{
name: "MyTextIndex"
}
)
.. TODO this probably should move out somewhere, but for now ...
.. _drop-text-index:
Use the Index Name to Drop a ``text`` Index
-------------------------------------------
Whether the :doc:`text </core/index-text>` index has the default name
or you specified a name for the :doc:`text </core/index-text>` index,
to drop the :doc:`text </core/index-text>` index, pass the index name
to the :method:`db.collection.dropIndex()` method.
For example, consider the index created by the following operation:
.. code-block:: javascript
db.collection.createIndex(
{
content: "text",
"users.comments": "text",
"users.profiles": "text"
},
{
name: "MyTextIndex"
}
)
Then, to remove this text index, pass the name ``"MyTextIndex"`` to the
:method:`db.collection.dropIndex()` method, as in the following:
.. code-block:: javascript
db.collection.dropIndex("MyTextIndex")
To get the names of the indexes, use the
:method:`db.collection.getIndexes()` method.