You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/cosmos-db/mongodb-indexing.md
+51-49Lines changed: 51 additions & 49 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
-
title: Indexing in Azure Cosmos DB's API for MongoDB
3
-
description: Presents an overview of the indexing capabilities with Azure Cosmos DB's API for MongoDB.
2
+
title: Manage indexing in Azure Cosmos DB's API for MongoDB
3
+
description: This article presents an overview of Azure Cosmos DB indexing capabilities using the MongoDB API.
4
4
ms.service: cosmos-db
5
5
ms.subservice: cosmosdb-mongo
6
6
ms.devlang: nodejs
@@ -10,41 +10,41 @@ author: timsander1
10
10
ms.author: tisande
11
11
12
12
---
13
-
# Indexing using Azure Cosmos DB's API for MongoDB
13
+
# Manage indexing in Azure Cosmos DB's API for MongoDB
14
14
15
-
Azure Cosmos DB's API for MongoDB leverages the core indexmanagement capabilities of Azure Cosmos DB. This document focuses on how to add indexes using Azure Cosmos DB's API for MongoDB. You can also read an [overview of indexing in Azure Cosmos DB](index-overview.md) that is relevant across all API's.
15
+
Azure Cosmos DB's API for MongoDB takes advantage of the core index-management capabilities of Azure Cosmos DB. This article focuses on how to add indexes using Azure Cosmos DB's API for MongoDB. You can also read an [overview of indexing in Azure Cosmos DB](index-overview.md) that's relevant across all APIs.
16
16
17
-
## Indexing for version 3.6
17
+
## Indexing for MongoDB server version 3.6
18
18
19
-
The `_id` field is always automatically indexed and cannot be dropped. Azure Cosmos DB's API for MongoDB automatically enforces uniqueness of the `_id` field per shard key.
19
+
Azure Cosmos DB's API for MongoDB server version 3.6 automatically indexes the `_id` field, which can't be dropped. It automatically enforces the uniqueness of the `_id` field per shard key.
20
20
21
-
To index additional fields, you should apply the MongoDB indexmanagement commands. As in MongoDB, only the `_id` field is indexed automatically. This default indexing policy is different from the Azure Cosmos DB SQL API, which indexes all fields by default.
21
+
To index additional fields, you apply the MongoDB index-management commands. As in MongoDB, Azure Cosmos DB's API for MongoDB automatically indexes the `_id` field only. This default indexing policy differs from the Azure Cosmos DB SQL API, which indexes all fields by default.
22
22
23
-
To apply a sort to a query, an index must be created on the fields used in the sort operation.
23
+
To apply a sort to a query, you must create an index on the fields used in the sort operation.
24
24
25
25
## Index types
26
26
27
27
### Single field
28
28
29
-
You can create indexes on any single field. The sort order of the single field index does not matter. The following command will create an index on the field `name`:
29
+
You can create indexes on any single field. The sort order of the single field index does not matter. The following command creates an index on the field `name`:
30
30
31
31
`db.coll.createIndex({name:1})`
32
32
33
-
One query will utilize multiple single field indexes, where available. You can create up to 500 single field indexes per container.
33
+
One query uses multiple single field indexes where available. You can create up to 500 single field indexes per container.
34
34
35
-
### Compound indexes (3.6)
35
+
### Compound indexes (MongoDB server version 3.6)
36
36
37
-
Compound indexes are supported for accounts using the 3.6 wire protocol. You can include up to 8 fields in a compound index. Unlike in MongoDB, you should only create a compound index if your query needs to sort efficiently on multiple fields at once. For queries with multiple filters that don't need to sort, you should create multiple single field indexes instead of one single compound index.
37
+
Azure Cosmos DB's API for MongoDB supports compound indexes for accounts that use the version 3.6 wire protocol. You can include up to eight fields in a compound index. Unlike in MongoDB, you should create a compound index only if your query needs to sort efficiently on multiple fields at once. For queries with multiple filters that don't need to sort, create multiple single field indexes instead of a single compound index.
38
38
39
-
The following command will create a compound index on the fields `name` and `age`:
39
+
The following command creates a compound index on the fields `name` and `age`:
40
40
41
41
`db.coll.createIndex({name:1,age:1})`
42
42
43
-
Compound indexes can be used to sort efficiently on multiple fields at once, such as:
43
+
You can use compound indexes to sort efficiently on multiple fields at once, as shown in the following example:
44
44
45
45
`db.coll.find().sort({name:1,age:1})`
46
46
47
-
The above compound index can also be used for efficient sorting on a query with the opposite sort order on all fields. Here's an example:
47
+
You can also use the preceding compound index to efficiently sort on a query with the opposite sort order on all fields. Here's an example:
48
48
49
49
`db.coll.find().sort({name:-1,age:-1})`
50
50
@@ -58,28 +58,28 @@ Azure Cosmos DB creates multikey indexes to index content stored in arrays. If y
58
58
59
59
### Geospatial indexes
60
60
61
-
Many geospatial operators will benefit from geospatial indexes. Currently, Azure Cosmos DB's API for MongoDB supports `2dsphere` indexes. `2d` indexes are not yet supported.
61
+
Many geospatial operators will benefit from geospatial indexes. Currently, Azure Cosmos DB's API for MongoDB supports `2dsphere` indexes. The API does not yet support `2d` indexes.
62
62
63
-
Here's an example for creating a geospatial index on the `location` field:
63
+
Here's an example of creating a geospatial index on the `location` field:
64
64
65
65
`db.coll.createIndex({ location : "2dsphere" })`
66
66
67
67
### Text indexes
68
68
69
-
Text indexes are not currently supported. For text search queries on strings, you should use [Azure Cognitive Search's](https://docs.microsoft.com/azure/search/search-howto-index-cosmosdb) integration with Azure Cosmos DB.
69
+
Azure Cosmos DB's API for MongoDB does not currently support text indexes. For text search queries on strings, you should use [Azure Cognitive Search](https://docs.microsoft.com/azure/search/search-howto-index-cosmosdb) integration with Azure Cosmos DB.
70
70
71
71
## Index properties
72
72
73
-
The following operations are common for both accounts serving wire protocol version 3.6 and accounts serving earlier wire protocol versions. You can also learn more about [supported indexes and indexed properties](mongodb-feature-support-36.md#indexes-and-index-properties).
73
+
The following operations are common for accounts serving wire protocol version 3.6 and accounts serving earlier versions. You can learn more about [supported indexes and indexed properties](mongodb-feature-support-36.md#indexes-and-index-properties).
74
74
75
75
### Unique indexes
76
76
77
-
[Unique indexes](unique-keys.md) are useful for enforcing that no two or more documents contain the same value for the indexed field(s).
77
+
[Unique indexes](unique-keys.md) are useful for enforcing that two or more documents do not contain the same value for indexed fields.
78
78
79
-
>[!Important]
79
+
>[!IMPORTANT]
80
80
> Unique indexes can be created only when the collection is empty (contains no documents).
81
81
82
-
The following command creates a unique index on the field "student_id":
82
+
The following command creates a unique index on the field `student_id`:
For sharded collections, creating a unique index requires providing the shard (partition) key. In other words, all unique indexes on a sharded collection are compound indexes where one of the fields is the partition key.
95
+
For sharded collections, you must provide the shard (partition) key to create a unique index. In other words, all unique indexes on a sharded collection are compound indexes where one of the fields is the partition key.
96
96
97
-
The following commands create a sharded collection ```coll``` (shard key is ```university```) with a unique index on fields student_id and university:
97
+
The following commands create a sharded collection ```coll``` (the shard key is ```university```) with a unique index on the fields `student_id` and `university`:
In the above example, if```"university":1``` clause was omitted, an error with the following message would be returned:
116
+
In the preceding example, omitting the ```"university":1``` clause returns an error with the following message:
117
117
118
118
```"cannot create unique index over {student_id : 1.0} with shard key pattern { university : 1.0 }"```
119
119
120
120
### TTL indexes
121
121
122
-
To enable document expiration in a particular collection, a ["TTL index" (time-to-live index)](../cosmos-db/time-to-live.md) needs to be created. A TTL index is an index on the _ts field with an "expireAfterSeconds" value.
122
+
To enable document expiration in a particular collection, you need to create a [time-to-live (TTL) index](../cosmos-db/time-to-live.md). A TTL index is an index on the `_ts` field with an `expireAfterSeconds` value.
The preceding commandwill cause the deletion of any documents in```db.coll``` collection that have not been modified in the last 10 seconds.
130
+
The preceding commanddeletes any documents in the```db.coll``` collection that have not been modified in the last 10 seconds.
131
131
132
132
> [!NOTE]
133
-
>**_ts** is an Azure Cosmos DB-specific field and is not accessible from MongoDB clients. It is a reserved (system) property that contains the timestamp of the document's last modification.
133
+
>The **_ts**field is specific to Azure Cosmos DBand is not accessible from MongoDB clients. It is a reserved (system) property that contains the time stamp of the document's last modification.
134
134
135
-
## Track the index progress
135
+
## Track index progress
136
136
137
-
The 3.6 version of Azure Cosmos DB's API forMongoDB accounts support the `currentOp()` command to track index progress on a database instance. This command returns a document that contains information about the in-progress operations on a database instance. The `currentOp` command is used to track all the in-progress operationsin native MongoDB whereas in Azure Cosmos DB's API for MongoDB, this command only supports tracking the index operation.
137
+
Version 3.6 of Azure Cosmos DB's API forMongoDB supports the `currentOp()` command to track index progress on a database instance. This command returns a document that contains information about in-progress operations on a database instance. You use the `currentOp` command to track all in-progress operationsin native MongoDB. In Azure Cosmos DB's API for MongoDB, this command only supports tracking the index operation.
138
138
139
-
Here are some examples that show how to use the `currentOp` command to track the index progress:
139
+
Here are some examples that show how to use the `currentOp` command to track index progress:
The index progress details contain percentage of progress for the current index operation. The following example shows the output document format for different stages of index progress:
159
+
### Examples of index progress output
160
160
161
-
1. If the index operation on a 'foo' collection and 'bar' database that has 60 % indexing complete will have the following output document. `Inprog[0].progress.total` shows 100 as the target completion.
161
+
The index progress details show the percentage of progress for the current index operation. Here's an example that shows the output document format for different stages of index progress:
162
+
163
+
- An index operation on a "foo" collection and "bar" database that is 60 percent complete will have the following output document. The `Inprog[0].progress.total` field shows 100 as the target completion percentage.
162
164
163
165
```json
164
166
{
@@ -182,7 +184,7 @@ The index progress details contain percentage of progress for the current index
182
184
}
183
185
```
184
186
185
-
2. For an index operation that has just started on a 'foo' collection and 'bar' database, the output document may show 0% progress until it reaches to a measurable level.
187
+
- If an index operation has just started on a "foo" collection and "bar" database, the output document might show 0 percent progress until it reaches a measurable level.
186
188
187
189
```json
188
190
{
@@ -206,7 +208,7 @@ The index progress details contain percentage of progress for the current index
206
208
}
207
209
```
208
210
209
-
3. When the in-progress index operation completes, the output document shows empty inprog operations.
211
+
- When the in-progress index operation finishes, the output document shows empty `inprog` operations.
210
212
211
213
```json
212
214
{
@@ -217,34 +219,34 @@ The index progress details contain percentage of progress for the current index
217
219
218
220
### Background index updates
219
221
220
-
Regardless of the value specified for the **Background** index property, index updates are always done in the background. Index updates consume RU's at a lower priority than other database operations. Therefore, index changes won't result in any downtime for writes, updates, or deletes.
222
+
Regardless of the value specified forthe **Background** index property, index updates are always donein the background. Because index updates consume Request Units (RUs) at a lower priority than other database operations, index changes won't result in any downtime for writes, updates, or deletes.
221
223
222
-
When adding a new index, queries will immediately utilize it. This means that queries may not return all the matching results, and will do so without returning any errors. Once the index transformation is completed, query results will be consistent. You can [track the index progress](#track-the-index-progress).
224
+
When you add a new index, queries will immediately use the index. This means that queries might not return all matching results and will do so without returning any errors. When the index transformation completes, query results will be consistent. You can [track index progress](#track-index-progress).
223
225
224
-
## Migrating collections with indexes
226
+
## Migrate collections with indexes
225
227
226
-
Currently, creating unique indexes is possible only when the collection contains no documents. Popular MongoDB migration tools attempt to create the unique indexes after importing the data. To circumvent this issue, it is suggested that users manually create the corresponding collections and unique indexes, instead of allowing the migration tool (for ```mongorestore``` this behavior is achieved by using the `--noIndexRestore` flag in the command line).
228
+
Currently, you can only create unique indexes when the collection contains no documents. Popular MongoDB migration tools try to create the unique indexes after importing the data. To circumvent this issue, you can manually create the corresponding collections and unique indexes instead of allowing the migration tool to try. (You can achieve this behavior for ```mongorestore``` by using the `--noIndexRestore` flag in the command line.)
227
229
228
-
## Indexing for version 3.2
230
+
## Indexing for MongoDB version 3.2
229
231
230
-
For Azure Cosmos DB accounts compatible with the 3.2 version of the MongoDB wire protocol, the available indexing features and defaults are different. You can [check your account's version](mongodb-feature-support-36.md#protocol-support). You can upgrade to the 3.6 version by filing a [support request](https://portal.azure.com/?#blade/Microsoft_Azure_Support/HelpAndSupportBlade).
232
+
Available indexing features and defaults are different for Azure Cosmos accounts that are compatible with version 3.2 of the MongoDB wire protocol. You can [check your account's version](mongodb-feature-support-36.md#protocol-support). You can upgrade to the 3.6 version by filing a [support request](https://portal.azure.com/?#blade/Microsoft_Azure_Support/HelpAndSupportBlade).
231
233
232
-
If you are using version 3.2, this section outlines key differences with version 3.6.
234
+
If you're using version 3.2, this section outlines key differences with version 3.6.
233
235
234
-
### Dropping the default indexes (3.2)
236
+
### Dropping default indexes (version 3.2)
235
237
236
-
Unlike the 3.6 version of Azure Cosmos DB's API for MongoDB, 3.2 version indexes every property by default. The following command can be used to drop these default indexes for a collection ```coll```:
238
+
Unlike the 3.6 version of Azure Cosmos DB's API for MongoDB, version 3.2 indexes every property by default. You can use the following command to drop these default indexes for a collection (```coll```):
After dropping the default indexes, you can add on additional indexes as done in Version 3.6.
245
+
After dropping the default indexes, you can add more indexes as you would inversion 3.6.
244
246
245
-
### Compound indexes (3.2)
247
+
### Compound indexes (version 3.2)
246
248
247
-
Compound indexes hold references to multiple fields of a document. If you would like to create a compound index, please upgrade to 3.6 version by filing a [support request](https://portal.azure.com/?#blade/Microsoft_Azure_Support/HelpAndSupportBlade).
249
+
Compound indexes hold references to multiple fields of a document. If you want to create a compound index, upgrade to version 3.6 by filing a [support request](https://portal.azure.com/?#blade/Microsoft_Azure_Support/HelpAndSupportBlade).
0 commit comments