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
|
// @tags: [requires_fastcount]
(function() {
"use strict";
const coll = db.find5;
coll.drop();
assert.commandWorked(coll.insert({a: 1}));
assert.commandWorked(coll.insert({b: 5}));
assert.eq(2, coll.find({}, {b: 1}).count(), "A");
function getIds(projection) {
return coll.find({}, projection).map(doc => doc._id).sort();
}
assert.eq(Array.tojson(getIds(null)), Array.tojson(getIds({})), "B1 ");
assert.eq(Array.tojson(getIds(null)), Array.tojson(getIds({a: 1})), "B2 ");
assert.eq(Array.tojson(getIds(null)), Array.tojson(getIds({b: 1})), "B3 ");
assert.eq(Array.tojson(getIds(null)), Array.tojson(getIds({c: 1})), "B4 ");
let results = coll.find({}, {a: 1}).sort({a: -1});
let first = results[0];
assert.eq(1, first.a, "C1");
assert.isnull(first.b, "C2");
let second = results[1];
assert.isnull(second.a, "C3");
assert.isnull(second.b, "C4");
results = coll.find({}, {b: 1}).sort({a: -1});
first = results[0];
assert.isnull(first.a, "C5");
assert.isnull(first.b, "C6");
second = results[1];
assert.isnull(second.a, "C7");
assert.eq(5, second.b, "C8");
assert(coll.drop());
assert.commandWorked(coll.insert({a: 1, b: {c: 2, d: 3, e: 4}}));
assert.eq(2, coll.findOne({}, {"b.c": 1}).b.c, "D");
const o = coll.findOne({}, {"b.c": 1, "b.d": 1});
assert(o.b.c, "E 1");
assert(o.b.d, "E 2");
assert(!o.b.e, "E 3");
assert(!coll.findOne({}, {"b.c": 1}).b.d, "F");
assert(coll.drop());
assert.commandWorked(coll.insert({a: {b: {c: 1}}}));
assert.eq(1, coll.findOne({}, {"a.b.c": 1}).a.b.c, "G");
}());
|