Skip to content

Commit ca39aa5

Browse files
Merge pull request #215605 from gahl-levy/mongo-nested-unique-compound-indexes
Update docs for new capability
2 parents cbd9aa4 + 203c442 commit ca39aa5

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

articles/cosmos-db/mongodb/how-to-configure-capabilities.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ms.reviewer: gahllevy
77
ms.service: cosmos-db
88
ms.subservice: mongodb
99
ms.topic: how-to
10-
ms.date: 10/12/2022
10+
ms.date: 10/24/2022
1111
ms.custom: ignite-2022
1212
---
1313

@@ -31,6 +31,8 @@ Capabilities are features that can be added or removed to your API for MongoDB a
3131
| `EnableMongoRoleBasedAccessControl` | Enable support for creating Users/Roles for native MongoDB role-based access control | No |
3232
| `EnableMongoRetryableWrites` | Enables support for retryable writes on the account | Yes |
3333
| `EnableMongo16MBDocumentSupport` | Enables support for inserting documents upto 16 MB in size | No |
34+
| `EnableUniqueCompoundNestedDocs` | Enables support for compound and unique indexes on nested fields, as long as the nested field is not an array. | No |
35+
3436

3537
## Enable a capability
3638

articles/cosmos-db/mongodb/indexing.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ms.service: cosmos-db
55
ms.subservice: mongodb
66
ms.devlang: javascript
77
ms.topic: how-to
8-
ms.date: 4/5/2022
8+
ms.date: 10/24/2022
99
author: gahl-levy
1010
ms.author: gahllevy
1111
ms.custom: devx-track-js, cosmos-db-video, ignite-2022
@@ -52,9 +52,22 @@ In the API for MongoDB, compound indexes are **required** if your query needs th
5252

5353
A compound index or single field indexes for each field in the compound index will result in the same performance for filtering in queries.
5454

55+
Compounded indexes on nested fields are not supported by default due to limiations with arrays. If your nested field does not contain an array, the index will work as intended. If your nested field contains an array (anywhere on the path), that value will be ignored in the index.
56+
57+
For example a compound index containing people.tom.age will work in this case since there's no array on the path:
58+
```javascript
59+
{ "people": { "tom": { "age": "25" }, "mark": { "age": "30" } } }
60+
```
61+
but won't won't work in this case since there's an array in the path:
62+
```javascript
63+
{ "people": { "tom": [ { "age": "25" } ], "mark": [ { "age": "30" } ] } }
64+
```
65+
66+
This feature can be enabled for your database account by [enabling the 'EnableUniqueCompoundNestedDocs' capability](how-to-configure-capabilities.md).
67+
5568

5669
> [!NOTE]
57-
> You can't create compound indexes on nested properties or arrays.
70+
> You can't create compound indexes on arrays.
5871
5972
The following command creates a compound index on the fields `name` and `age`:
6073

@@ -245,6 +258,20 @@ In the preceding example, omitting the ```"university":1``` clause returns an er
245258
246259
Unique indexes need to be created while the collection is empty.
247260
261+
Unique indexes on nested fields are not supported by default due to limiations with arrays. If your nested field does not contain an array, the index will work as intended. If your nested field contains an array (anywhere on the path), that value will be ignored in the unique index and uniqueness wil not be preserved for that value.
262+
263+
For example a unique index on people.tom.age will work in this case since there's no array on the path:
264+
```javascript
265+
{ "people": { "tom": { "age": "25" }, "mark": { "age": "30" } } }
266+
```
267+
but won't won't work in this case since there's an array in the path:
268+
```javascript
269+
{ "people": { "tom": [ { "age": "25" } ], "mark": [ { "age": "30" } ] } }
270+
```
271+
272+
This feature can be enabled for your database account by [enabling the 'EnableUniqueCompoundNestedDocs' capability](how-to-configure-capabilities.md).
273+
274+
248275
### TTL indexes
249276
250277
To enable document expiration in a particular collection, you need to create a [time-to-live (TTL) index](../time-to-live.md). A TTL index is an index on the `_ts` field with an `expireAfterSeconds` value.

0 commit comments

Comments
 (0)