forked from mongodb/mongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucketAuto_concatArrays.js
69 lines (64 loc) · 2.23 KB
/
bucketAuto_concatArrays.js
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
/**
* Tests that $concatArrays under $bucketAuto preserves incoming document order.
* @tags: [requires_fcv_81]
*/
const coll = db[jsTestName()];
coll.drop();
const docs = [];
for (let i = 0; i < 10; i++) {
docs.push({_id: i, n: 9 - i, arr: [i, 42]});
}
coll.insertMany(docs);
{
// One bucket, all the documents coming out of the $bucketAuto stage should be in the same
// order as they came in (i.e. sorted by _id).
const res =
coll.aggregate([
{$sort: {_id: 1}},
{$bucketAuto: {groupBy: "$n", buckets: 1, output: {nums: {$concatArrays: "$arr"}}}}
])
.toArray();
const expected = [{
"_id": {"min": 0, "max": 9},
"nums": [0, 42, 1, 42, 2, 42, 3, 42, 4, 42, 5, 42, 6, 42, 7, 42, 8, 42, 9, 42]
}];
assert.eq(
res,
expected,
"$bucketAuto with 1 bucket & $concatArrays does not obey sort order of incoming docs.");
}
{
// Two buckets, the documents in each bucket should be sorted by _id as well.
const res =
coll.aggregate([
{$sort: {_id: 1}},
{$bucketAuto: {groupBy: "$n", buckets: 2, output: {nums: {$concatArrays: "$arr"}}}}
])
.toArray();
const expected = [
{"_id": {"min": 0, "max": 5}, "nums": [5, 42, 6, 42, 7, 42, 8, 42, 9, 42]},
{"_id": {"min": 5, "max": 9}, "nums": [0, 42, 1, 42, 2, 42, 3, 42, 4, 42]}
];
assert.eq(
res,
expected,
"$bucketAuto with 2 buckets & $concatArrays does not obey sort order of incoming docs.");
}
{
// Ensure that if we try to $concatArrays something that evaluates to 'missing', no value gets
// appended to the resulting array.
const res =
coll.aggregate([{
"$bucketAuto": {
"groupBy": "$a",
"buckets": 1,
"output":
{"array": {"$concatArrays": {$getField: {"field": "b", "input": {a: 0}}}}}
}
}])
.toArray();
const expected = [{"_id": {"min": null, "max": null}, "array": []}];
assert.eq(res,
expected,
"$bucketAuto with $concatArrays does not append any missing values as null.");
}