1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
// Tests for $exists against documents that store a null value
//
// A document with a missing value for an indexed field
// is indexed *as if* it had the value 'null' explicitly.
// Therefore:
// { b : 1 }
// { a : null, b : 1 }
// look identical based on a standard index on { a : 1 }.
//
// -- HOWEVER!! --
// A sparse index on { a : 1 } would include { a : null, b : 1 },
// but would not include { b : 1 }. In this case, the two documents
// are treated equally.
//
// Also, super special edge case around sparse, compound indexes
// from Mathias:
// If we have a sparse index on { a : 1, b : 1 }
// And we insert docs {}, { a : 1 },
// { b : 1 }, and { a : 1, b : 1 }
// everything but {} will have an index entry.
// Let's make sure we handle this properly!
t = db.jstests_existsb;
t.drop();
t.save({});
t.save({a: 1});
t.save({b: 1});
t.save({a: 1, b: null});
t.save({a: 1, b: 1});
/** run a series of checks, just on the number of docs found */
function checkExistsNull() {
// Basic cases
assert.eq(3, t.count({a: {$exists: true}}));
assert.eq(2, t.count({a: {$exists: false}}));
assert.eq(3, t.count({b: {$exists: true}}));
assert.eq(2, t.count({b: {$exists: false}}));
// With negations
assert.eq(3, t.count({a: {$not: {$exists: false}}}));
assert.eq(2, t.count({a: {$not: {$exists: true}}}));
assert.eq(3, t.count({b: {$not: {$exists: false}}}));
assert.eq(2, t.count({b: {$not: {$exists: true}}}));
// Both fields
assert.eq(2, t.count({a: 1, b: {$exists: true}}));
assert.eq(1, t.count({a: 1, b: {$exists: false}}));
assert.eq(1, t.count({a: {$exists: true}, b: 1}));
assert.eq(1, t.count({a: {$exists: false}, b: 1}));
// Both fields, both $exists
assert.eq(2, t.count({a: {$exists: true}, b: {$exists: true}}));
assert.eq(1, t.count({a: {$exists: true}, b: {$exists: false}}));
assert.eq(1, t.count({a: {$exists: false}, b: {$exists: true}}));
assert.eq(1, t.count({a: {$exists: false}, b: {$exists: false}}));
}
// with no index, make sure we get correct results
checkExistsNull();
// try with a standard index
t.ensureIndex({a: 1});
checkExistsNull();
// try with a sparse index
t.dropIndexes();
t.ensureIndex({a: 1}, {sparse: true});
checkExistsNull();
// try with a compound index
t.dropIndexes();
t.ensureIndex({a: 1, b: 1});
checkExistsNull();
// try with sparse compound index
t.dropIndexes();
t.ensureIndex({a: 1, b: 1}, {sparse: true});
checkExistsNull();
|