summaryrefslogtreecommitdiff
path: root/jstests/core/ne_array.js
blob: 891bf0d2e91951e6701d6dc4f00b3dc0d88256ba (plain)
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
// This is a test for the query correctness bug described in SERVER-38949. A {$ne: <array>} query
// cannot "naively" use an index. That is, it cannot use an index by simply generating bounds for
// {$eq: <array>} query and then complementing them. This test checks that the correct results are
// returned for this type of query when an index is present.
// @tags: [
//   requires_non_retryable_writes,
// ]
(function() {
const coll = db.ne_array;
coll.drop();
assert.commandWorked(coll.createIndex({a: 1}));

assert.commandWorked(coll.insert({_id: 0, a: [1]}));
assert.commandWorked(coll.insert({_id: 1, a: [1, 3]}));

assert.eq(coll.find({a: {$ne: [1, 3]}}, {_id: 1}).toArray(), [{_id: 0}]);
assert.eq(coll.find({a: {$ne: [1]}}, {_id: 1}).toArray(), [{_id: 1}]);

assert.eq(coll.find({a: {$not: {$in: [[1]]}}}, {_id: 1}).toArray(), [{_id: 1}]);
assert.eq(coll.find({a: {$not: {$in: [[1, 3]]}}}, {_id: 1}).toArray(), [{_id: 0}]);
assert.eq(coll.find({a: {$not: {$in: [[1], [1, 3]]}}}, {_id: 1}).toArray(), []);
assert.eq(coll.find({a: {$not: {$in: ["scalar value", [1, 3]]}}}, {_id: 1}).toArray(), [{_id: 0}]);

// Insert some documents which have nested arrays so we can test $elemMatch value.
assert.commandWorked(coll.remove({}));
assert.commandWorked(coll.insert({_id: 0, a: [[123]]}));
assert.commandWorked(coll.insert({_id: 1, a: [4, 5, [123]]}));
assert.commandWorked(coll.insert({_id: 2, a: [7, 8]}));

// sort by _id in case we run on a sharded cluster which puts the documents on different
// shards (and thus, might return them in any order).
assert.eq(coll.find({a: {$elemMatch: {$not: {$eq: [123]}}}}, {_id: 1}).sort({_id: 1}).toArray(),
          [{_id: 1}, {_id: 2}]);

assert.eq(coll.find({a: {$elemMatch: {$not: {$in: [[123]]}}}}, {_id: 1}).sort({_id: 1}).toArray(),
          [{_id: 1}, {_id: 2}]);

assert.eq(coll.find({a: {$not: {$elemMatch: {$eq: [123]}}}}, {_id: 1}).toArray(), [{_id: 2}]);
assert.eq(coll.find({a: {$not: {$elemMatch: {$in: [[123]]}}}}, {_id: 1}).toArray(), [{_id: 2}]);

// Test $elemMatch object.
assert.commandWorked(coll.remove({}));
coll.dropIndexes();
assert.commandWorked(coll.createIndex({"a.b": 1}));
assert.commandWorked(coll.insert({_id: 0, a: [[123]]}));
assert.commandWorked(coll.insert({_id: 1, a: [{b: 123}]}));
assert.commandWorked(coll.insert({_id: 2, a: [{b: [4, [123]]}]}));
assert.commandWorked(coll.insert({_id: 3, a: [{b: [[123]]}]}));

// Remember that $ne with an array will match arrays where _none_ of the elements match.
assert.eq(coll.find({a: {$elemMatch: {b: {$ne: [123]}}}}, {_id: 1}).sort({_id: 1}).toArray(),
          [{_id: 0}, {_id: 1}]);
assert.eq(
    coll.find({a: {$elemMatch: {b: {$not: {$in: [[123]]}}}}}, {_id: 1}).sort({_id: 1}).toArray(),
    [{_id: 0}, {_id: 1}]);

assert.eq(coll.find({a: {$not: {$elemMatch: {b: [123]}}}}, {_id: 1}).sort({_id: 1}).toArray(),
          [{_id: 0}, {_id: 1}]);
assert.eq(
    coll.find({a: {$not: {$elemMatch: {b: {$in: [[123]]}}}}}, {_id: 1}).sort({_id: 1}).toArray(),
    [{_id: 0}, {_id: 1}]);
})();