@@ -182,17 +182,17 @@ value of ``{ "a.loc": "B", "a.qty": null }``.
182
182
183
183
.. _unique-index-and-missing-field:
184
184
185
- Unique Index and Missing Field
186
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
185
+ Missing Document Field in a Unique Single- Field Index
186
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
187
187
188
- If a document does not have a value for the indexed field in a unique
189
- index, the index will store a null value for this document. Because of
190
- the unique constraint, MongoDB will only permit one document that lacks
191
- the indexed field. If there is more than one document without a value
192
- for the indexed field or is missing the indexed field , the index build
193
- will fail with a duplicate key error.
188
+ If a document has a ``null`` or missing value for the indexed field in a unique
189
+ single-field index, the index stores a `` null`` value for that document.
190
+ Because of the unique constraint, a single-field unique index can only
191
+ contain one document that contains a ``null`` value in its index entry. If there is
192
+ more than one document with a ``null`` value in its index entry , the index
193
+ build fails with a duplicate key error.
194
194
195
- For example, a collection has a unique index on ``x``:
195
+ For example, a collection has a unique single-field index on ``x``:
196
196
197
197
.. code-block:: javascript
198
198
@@ -206,9 +206,8 @@ field ``x``:
206
206
207
207
db.collection.insert( { y: 1 } )
208
208
209
- However, the unique index errors on the insertion of a document without
210
- the field ``x`` if the collection already contains a document missing
211
- the field ``x``:
209
+ However, you cannot insert a document without the field ``x`` if the
210
+ collection already contains a document missing the field ``x``:
212
211
213
212
.. code-block:: javascript
214
213
@@ -227,11 +226,95 @@ the unique constraint on the value of the field ``x``:
227
226
}
228
227
})
229
228
230
- .. seealso: :
229
+ .. _unique-partial-indexes :
231
230
232
- :ref:`unique-partial-indexes`
231
+ Missing Document Fields in a Unique Compound Index
232
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
233
+
234
+ If a document has a ``null`` or missing value for one or more indexed
235
+ fields in a unique compound index, the index stores a null value for
236
+ each ``null`` or missing field in the document's index entry. Because of
237
+ the unique constraint, a unique compound index only permits one document
238
+ that has a ``null`` value for all indexed fields in an index entry. If
239
+ there is more than one index entry with a ``null`` value for all indexed
240
+ fields, the index build fails with a duplicate key error. MongoDB
241
+ permits multiple documents with missing fields in unique compound
242
+ indexes as long as each index entry is unique.
243
+
244
+ For example, a collection ``students`` has a unique compound index on fields
245
+ ``name``, ``age``, and ``grade``:
246
+
247
+ .. code-block:: javascript
248
+
249
+ db.students.createIndex(
250
+ {
251
+ "name": 1,
252
+ "age": -1,
253
+ "grade": 1
254
+ },
255
+ { unique: true }
256
+ )
257
+
258
+ If the collection does not already contain identical documents, the
259
+ unique compound index allows the insertion of the following documents
260
+ that are all missing the ``grade`` field.
261
+
262
+ .. code-block:: javascript
263
+
264
+ db.students.insertMany(
265
+ { "name": "Meredith", "age": 12 },
266
+ { "name": "Olivia", "age": 11 },
267
+ { "name": "Benjamin" }
268
+ )
269
+
270
+ However, you cannot insert a document that has the same index key (value
271
+ for ``name``, ``age``, and ``grade``) as another document in the
272
+ collection.
273
+
274
+ .. code-block:: javascript
275
+
276
+ db.students.insertOne( { name: "Meredith", age: 12 } )
277
+
278
+ The operation fails to insert the document because of the violation of
279
+ the unique constraint on the values of the fields ``name``, ``age``, and ``grade``:
280
+
281
+ .. code-block:: javascript
282
+
283
+ WriteResult({
284
+ "nInserted" : 0,
285
+ "writeError" : {
286
+ "code" : 11000,
287
+ "errmsg" :
288
+ "E11000 duplicate key error collection: test.students
289
+ index: name_1_age_-1_grade_1
290
+ dup key: { name: "Meredith", age: 12, grade: null }
291
+ }
292
+ } )
293
+
294
+ You also cannot insert a document that is unique but shares an index
295
+ key with an existing index entry.
296
+
297
+ .. code-block:: javascript
298
+
299
+ db.students.insertOne( { name: "Olivia", "age": 11, "favorite color": "red"} )
300
+
301
+ The operation fails to insert the document because of the violation of
302
+ the unique constraint on the values of the fields ``name``, ``age``, and
303
+ ``grade``:
304
+
305
+ .. code-block:: javascript
306
+
307
+ WriteResult({
308
+ "nInserted" : 0,
309
+ "writeError" : {
310
+ "code" : 11000,
311
+ "errmsg" :
312
+ "E11000 duplicate key error collection: test.students
313
+ index: name_1_age_-1_grade_1
314
+ dup key: { name: "Olivia", age: 11, grade: null }
315
+ }
316
+ } )
233
317
234
- .. _unique-partial-indexes:
235
318
236
319
Unique Partial Indexes
237
320
~~~~~~~~~~~~~~~~~~~~~~
0 commit comments