@@ -284,3 +284,45 @@ The unique index constraints mean that:
284
284
285
285
- For an already-sharded collection, you cannot create unique indexes
286
286
on other fields.
287
+
288
+ Sparse and Non-Sparse Unique Indexes
289
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
290
+
291
+ .. include:: /includes/fact-5.0-sparse-unique-index-updates.rst
292
+
293
+ Basic and Unique Indexes With Duplicate Key Patterns
294
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
295
+
296
+ Starting in MongoDB 5.0, basic and unique indexes can exist with the
297
+ same :ref:`key pattern <key_patterns>`.
298
+
299
+ This duplication in key patterns allows for adding a unique index to
300
+ already indexed fields.
301
+
302
+ In this example:
303
+
304
+ Create a basic index with the key pattern ``{ score : 1 }`` and insert
305
+ three documents.
306
+
307
+ .. code-block:: javascript
308
+
309
+ db.scoreHistory.createIndex( { score : 1 }, { name: "basic_index" } )
310
+ db.scoreHistory.insert( { score : 1 } )
311
+ db.scoreHistory.insert( { score : 2 } )
312
+ db.scoreHistory.insert( { score : 3 } )
313
+
314
+ Create a unique index with the same key pattern ``{ score : 1 }``.
315
+
316
+ .. code-block:: javascript
317
+
318
+ db.scoreHistory.createIndex( { score : 1 }, { name: "unique_index", unique: true } )
319
+
320
+ Try to insert a duplicate ``score`` document that fails because of
321
+ the unique index.
322
+
323
+ .. code-block:: javascript
324
+
325
+ db.scoreHistory.insert( { score : 3 } )
326
+
327
+
328
+
0 commit comments