@@ -322,8 +322,46 @@ value `bar` will be inserted only once:
322
322
db.posts.insert({ tags: [ "foobar", "bar", "bar" ] });
323
323
```
324
324
325
- If an array index is declared unique, the de-duplication of array values will happen before
325
+ If an array index is declared ** unique** , the de-duplication of array values will happen before
326
326
inserting the values into the index, so the above insert operation will not necessarily fail.
327
327
It will fail if the index already contains an instance of the `bar` value, but will succeed
328
328
if the value `bar` is not already present in the index.
329
329
330
+ If an array index is declared and you store documents that do not have an array at the specified attribute
331
+ this document will not be inserted in the index. Hence the following objects will not be indexed:
332
+
333
+ ```js
334
+ db.posts.ensureIndex({ type: "hash", fields: [ "tags[*]" ] });
335
+ db.posts.insert({ something: "else" });
336
+ db.posts.insert({ tags: null });
337
+ db.posts.insert({ tags: "this is no array" });
338
+ db.posts.insert({ tags: { content: [1, 2, 3] } });
339
+ ```
340
+
341
+ An array index is able to index an explicit `null` value and when queried for it, it will only
342
+ return those documents having explicitly `null` stored in the array, it will not return any
343
+ documents that do not have the array at all.
344
+
345
+ ```js
346
+ db.posts.ensureIndex({ type: "hash", fields: [ "tags[*]" ] });
347
+ db.posts.insert({tags: null}) // Will not be indexed
348
+ db.posts.insert({tags: []}) // Will not be indexed
349
+ db.posts.insert({tags: [null]}); // Will be indexed for null
350
+ db.posts.insert({tags: [null, 1, 2]}); // Will be indexed for null, 1 and 2
351
+ ```
352
+
353
+ Declaring an array index as **sparse** does not have an effect on the array part of the index,
354
+ this in particular means that explicit `null` values are also indexed in the **sparse** version.
355
+ If an index is combined from an array and a normal attribute the sparsity will apply for the attribute e.g.:
356
+
357
+ ```js
358
+ db.posts.ensureIndex({ type: "hash", fields: [ "tags[*]", "name" ], sparse: true });
359
+ db.posts.insert({tags: null, name: "alice"}) // Will not be indexed
360
+ db.posts.insert({tags: [], name: "alice"}) // Will not be indexed
361
+ db.posts.insert({tags: [1, 2, 3]}) // Will not be indexed
362
+ db.posts.insert({tags: [1, 2, 3], name: null}) // Will not be indexed
363
+ db.posts.insert({tags: [1, 2, 3], name: "alice"})
364
+ // Will be indexed for [1, "alice"], [2, "alice"], [3, "alice"]
365
+ db.posts.insert({tags: [null], name: "bob"})
366
+ // Will be indexed for [null, "bob"]
367
+ ```
0 commit comments