summaryrefslogtreecommitdiff
path: root/jstests/core/objectfind.js
blob: 452da414256b5acb10a1ca3c0e49561a4c3df82f (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
 * Tests some of the find command's semantics with respect to object comparisons.
 */
(function() {
"use strict";

load("jstests/aggregation/extras/utils.js");  // arrayEq

const t = db.jstests_objectfind;
t.drop();

function runWithAndWithoutIndex(keyPattern, testFunc) {
    testFunc();
    assert.commandWorked(t.createIndex(keyPattern));
    testFunc();
}

assert.commandWorked(t.insert([
    {a: {b: "d", c: "e"}},
    {a: {c: "b", e: "d"}},
    {a: {c: "b", e: "d", h: "g"}},
    {a: {c: "b", e: {a: "a"}}},
    {a: {c: "d", e: "b"}},
    {a: {d: "b", e: "c"}},
    {a: {d: "c"}},
    {a: {d: "c", b: "e"}},
    {a: {d: "c", e: "b"}},
    {a: {d: "c", e: "b", g: "h"}},
    {a: {d: "c", e: "f"}},
    {a: {d: "e", b: "c"}},
    {a: {d: "e", c: "b"}},
    {a: {e: "b", d: "c"}},
    {a: {e: "d", c: "b"}}
]));

runWithAndWithoutIndex({a: 1}, () => {
    assert(
        arrayEq(t.find({a: {$eq: {d: "c", e: "b"}}}, {_id: 0}).toArray(), [{a: {d: "c", e: "b"}}]));

    assert(arrayEq(t.find({a: {$lt: {d: "c", e: "b"}}}, {_id: 0}).toArray(), [
        {a: {b: "d", c: "e"}},
        {a: {c: "b", e: "d"}},
        {a: {c: "b", e: "d", h: "g"}},
        {a: {c: "b", e: {a: "a"}}},
        {a: {c: "d", e: "b"}},
        {a: {d: "b", e: "c"}},
        {a: {d: "c"}},
        {a: {d: "c", b: "e"}},
    ]));

    assert(arrayEq(t.find({a: {$gt: {d: "c", e: "b"}}}, {_id: 0}).toArray(), [
        {a: {d: "c", e: "b", g: "h"}},
        {a: {d: "c", e: "f"}},
        {a: {d: "e", b: "c"}},
        {a: {d: "e", c: "b"}},
        {a: {e: "b", d: "c"}},
        {a: {e: "d", c: "b"}}
    ]));

    assert(
        arrayEq(t.find({a: {$eq: {c: "b", e: "d"}}}, {_id: 0}).toArray(), [{a: {c: "b", e: "d"}}]));

    assert(
        arrayEq(t.find({a: {$lt: {c: "b", e: "d"}}}, {_id: 0}).toArray(), [{a: {b: "d", c: "e"}}]));

    assert(arrayEq(t.find({a: {$gt: {c: "b", e: "d"}}}, {_id: 0}).toArray(), [
        {a: {c: "b", e: "d", h: "g"}},
        {a: {c: "b", e: {a: "a"}}},
        {a: {c: "d", e: "b"}},
        {a: {d: "b", e: "c"}},
        {a: {d: "c"}},
        {a: {d: "c", b: "e"}},
        {a: {d: "c", e: "b"}},
        {a: {d: "c", e: "b", g: "h"}},
        {a: {d: "c", e: "f"}},
        {a: {d: "e", b: "c"}},
        {a: {d: "e", c: "b"}},
        {a: {e: "b", d: "c"}},
        {a: {e: "d", c: "b"}}
    ]));

    assert.eq(0, t.find({a: {$eq: {}}}).itcount());
    assert.eq(0, t.find({a: {$lt: {}}}).itcount());
    assert.eq(15, t.find({a: {$gt: {}}}).itcount());
});
})();