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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
|
(function() {
"use strict";
// Loads data into the namespace 'aggdb.articles'.
load('jstests/aggregation/data/articles.js');
load('jstests/aggregation/extras/utils.js');
const testDB = db.getSiblingDB("aggdb");
// just passing through fields
let p1 = testDB.runCommand({
aggregate: "article",
pipeline: [{$project: {tags: 1, pageViews: 1}}, {$sort: {_id: 1}}],
cursor: {}
});
let p1result = [
{"_id": 1, "pageViews": 5, "tags": ["fun", "good", "fun"]},
{"_id": 2, "pageViews": 7, "tags": ["fun", "nasty"]},
{"_id": 3, "pageViews": 6, "tags": ["nasty", "filthy"]}
];
assert.docEq(p1.cursor.firstBatch, p1result, 'p1 failed');
// a simple array unwinding
let u1 = testDB.runCommand({aggregate: "article", pipeline: [{$unwind: "$tags"}], cursor: {}});
let u1result = [
{
"_id": 1,
"title": "this is my title",
"author": "bob",
"posted": ISODate("2004-03-21T18:59:54Z"),
"pageViews": 5,
"tags": "fun",
"comments":
[{"author": "joe", "text": "this is cool"}, {"author": "sam", "text": "this is bad"}],
"other": {"foo": 5}
},
{
"_id": 1,
"title": "this is my title",
"author": "bob",
"posted": ISODate("2004-03-21T18:59:54Z"),
"pageViews": 5,
"tags": "good",
"comments":
[{"author": "joe", "text": "this is cool"}, {"author": "sam", "text": "this is bad"}],
"other": {"foo": 5}
},
{
"_id": 1,
"title": "this is my title",
"author": "bob",
"posted": ISODate("2004-03-21T18:59:54Z"),
"pageViews": 5,
"tags": "fun",
"comments":
[{"author": "joe", "text": "this is cool"}, {"author": "sam", "text": "this is bad"}],
"other": {"foo": 5}
},
{
"_id": 2,
"title": "this is your title",
"author": "dave",
"posted": ISODate("2030-08-08T04:11:10Z"),
"pageViews": 7,
"tags": "fun",
"comments": [
{"author": "barbara", "text": "this is interesting"},
{"author": "jenny", "text": "i like to play pinball", "votes": 10}
],
"other": {"bar": 14}
},
{
"_id": 2,
"title": "this is your title",
"author": "dave",
"posted": ISODate("2030-08-08T04:11:10Z"),
"pageViews": 7,
"tags": "nasty",
"comments": [
{"author": "barbara", "text": "this is interesting"},
{"author": "jenny", "text": "i like to play pinball", "votes": 10}
],
"other": {"bar": 14}
},
{
"_id": 3,
"title": "this is some other title",
"author": "jane",
"posted": ISODate("2000-12-31T05:17:14Z"),
"pageViews": 6,
"tags": "nasty",
"comments": [
{"author": "will", "text": "i don't like the color"},
{"author": "jenny", "text": "can i get that in green?"}
],
"other": {"bar": 14}
},
{
"_id": 3,
"title": "this is some other title",
"author": "jane",
"posted": ISODate("2000-12-31T05:17:14Z"),
"pageViews": 6,
"tags": "filthy",
"comments": [
{"author": "will", "text": "i don't like the color"},
{"author": "jenny", "text": "can i get that in green?"}
],
"other": {"bar": 14}
}
];
let firstBatch = u1.cursor.firstBatch;
assert(arrayEq(firstBatch, u1result), tojson({got: firstBatch, expected: u1result}));
// unwind an array at the end of a dotted path
testDB.ut.drop();
assert.writeOK(testDB.ut.insert({_id: 4, a: 1, b: {e: 7, f: [4, 3, 2, 1]}, c: 12, d: 17}));
let u2 = testDB.runCommand(
{aggregate: "ut", pipeline: [{$unwind: "$b.f"}, {$sort: {"b.f": -1}}], cursor: {}});
let u2result = [
{"_id": 4, "a": 1, "b": {"e": 7, "f": 4}, "c": 12, "d": 17},
{"_id": 4, "a": 1, "b": {"e": 7, "f": 3}, "c": 12, "d": 17},
{"_id": 4, "a": 1, "b": {"e": 7, "f": 2}, "c": 12, "d": 17},
{"_id": 4, "a": 1, "b": {"e": 7, "f": 1}, "c": 12, "d": 17}
];
assert.docEq(u2.cursor.firstBatch, u2result, 'u2 failed');
// combining a projection with unwinding an array
let p2 = testDB.runCommand({
aggregate: "article",
pipeline: [{$project: {author: 1, tags: 1, pageViews: 1}}, {$unwind: "$tags"}],
cursor: {}
});
let p2result = [
{"_id": 1, "author": "bob", "pageViews": 5, "tags": "fun"},
{"_id": 1, "author": "bob", "pageViews": 5, "tags": "good"},
{"_id": 1, "author": "bob", "pageViews": 5, "tags": "fun"},
{"_id": 2, "author": "dave", "pageViews": 7, "tags": "fun"},
{"_id": 2, "author": "dave", "pageViews": 7, "tags": "nasty"},
{"_id": 3, "author": "jane", "pageViews": 6, "tags": "nasty"},
{"_id": 3, "author": "jane", "pageViews": 6, "tags": "filthy"}
];
firstBatch = p2.cursor.firstBatch;
assert(arrayEq(firstBatch, p2result), tojson({got: firstBatch, expected: p2result}));
// pulling values out of subdocuments
let p3 = testDB.runCommand({
aggregate: "article",
pipeline: [{$project: {otherfoo: "$other.foo", otherbar: "$other.bar"}}, {$sort: {_id: 1}}],
cursor: {}
});
let p3result =
[{"_id": 1, "otherfoo": 5}, {"_id": 2, "otherbar": 14}, {"_id": 3, "otherbar": 14}];
assert.docEq(p3.cursor.firstBatch, p3result, 'p3 failed');
// projection includes a computed value
let p4 = testDB.runCommand({
aggregate: "article",
pipeline:
[{$project: {author: 1, daveWroteIt: {$eq: ["$author", "dave"]}}}, {$sort: {_id: 1}}],
cursor: {}
});
let p4result = [
{"_id": 1, "author": "bob", "daveWroteIt": false},
{"_id": 2, "author": "dave", "daveWroteIt": true},
{"_id": 3, "author": "jane", "daveWroteIt": false}
];
assert.docEq(p4.cursor.firstBatch, p4result, 'p4 failed');
// projection includes a virtual (fabricated) document
let p5 = testDB.runCommand({
aggregate: "article",
pipeline: [
{$project: {author: 1, pageViews: 1, tags: 1}},
{$unwind: "$tags"},
{$project: {author: 1, subDocument: {foo: "$pageViews", bar: "$tags"}}}
],
cursor: {}
});
let p5result = [
{"_id": 1, "author": "bob", "subDocument": {"foo": 5, "bar": "fun"}},
{"_id": 1, "author": "bob", "subDocument": {"foo": 5, "bar": "good"}},
{"_id": 1, "author": "bob", "subDocument": {"foo": 5, "bar": "fun"}},
{"_id": 2, "author": "dave", "subDocument": {"foo": 7, "bar": "fun"}},
{"_id": 2, "author": "dave", "subDocument": {"foo": 7, "bar": "nasty"}},
{"_id": 3, "author": "jane", "subDocument": {"foo": 6, "bar": "nasty"}},
{"_id": 3, "author": "jane", "subDocument": {"foo": 6, "bar": "filthy"}}
];
firstBatch = p5.cursor.firstBatch;
assert(arrayEq(firstBatch, p5result), tojson({got: firstBatch, expected: p5result}));
// multi-step aggregate
// nested expressions in computed fields
let p6 = testDB.runCommand({
aggregate: "article",
pipeline: [
{$project: {author: 1, tags: 1, pageViews: 1}},
{$unwind: "$tags"},
{
$project: {
author: 1,
tag: "$tags",
pageViews: 1,
daveWroteIt: {$eq: ["$author", "dave"]},
weLikeIt: {$or: [{$eq: ["$author", "dave"]}, {$eq: ["$tags", "good"]}]}
}
}
],
cursor: {}
});
let p6result = [
{
"_id": 1,
"author": "bob",
"pageViews": 5,
"tag": "fun",
"daveWroteIt": false,
"weLikeIt": false
},
{
"_id": 1,
"author": "bob",
"pageViews": 5,
"tag": "good",
"daveWroteIt": false,
"weLikeIt": true
},
{
"_id": 1,
"author": "bob",
"pageViews": 5,
"tag": "fun",
"daveWroteIt": false,
"weLikeIt": false
},
{
"_id": 2,
"author": "dave",
"pageViews": 7,
"tag": "fun",
"daveWroteIt": true,
"weLikeIt": true
},
{
"_id": 2,
"author": "dave",
"pageViews": 7,
"tag": "nasty",
"daveWroteIt": true,
"weLikeIt": true
},
{
"_id": 3,
"author": "jane",
"pageViews": 6,
"tag": "nasty",
"daveWroteIt": false,
"weLikeIt": false
},
{
"_id": 3,
"author": "jane",
"pageViews": 6,
"tag": "filthy",
"daveWroteIt": false,
"weLikeIt": false
}
];
firstBatch = p6.cursor.firstBatch;
assert(arrayEq(firstBatch, p6result), tojson({got: firstBatch, expected: p6result}));
// slightly more complex computed expression; $ifNull
let p7 = testDB.runCommand({
aggregate: "article",
pipeline: [
{$project: {theSum: {$add: ["$pageViews", {$ifNull: ["$other.foo", "$other.bar"]}]}}},
{$sort: {_id: 1}}
],
cursor: {}
});
let p7result = [{"_id": 1, "theSum": 10}, {"_id": 2, "theSum": 21}, {"_id": 3, "theSum": 20}];
assert.docEq(p7.cursor.firstBatch, p7result, 'p7 failed');
// dotted path inclusion; _id exclusion
let p8 = testDB.runCommand({
aggregate: "article",
pipeline:
[{$project: {_id: 0, author: 1, tags: 1, "comments.author": 1}}, {$unwind: "$tags"}],
cursor: {}
});
let p8result = [
{"author": "bob", "tags": "fun", "comments": [{"author": "joe"}, {"author": "sam"}]},
{"author": "bob", "tags": "good", "comments": [{"author": "joe"}, {"author": "sam"}]},
{"author": "bob", "tags": "fun", "comments": [{"author": "joe"}, {"author": "sam"}]},
{"author": "dave", "tags": "fun", "comments": [{"author": "barbara"}, {"author": "jenny"}]},
{
"author": "dave",
"tags": "nasty",
"comments": [{"author": "barbara"}, {"author": "jenny"}]
},
{"author": "jane", "tags": "nasty", "comments": [{"author": "will"}, {"author": "jenny"}]},
{
"author": "jane",
"tags": "filthy",
"comments": [{"author": "will"}, {"author": "jenny"}]
}
];
firstBatch = p8.cursor.firstBatch;
assert(arrayEq(firstBatch, p8result), tojson({got: firstBatch, expected: p8result}));
// collapse a dotted path with an intervening array
let p9 = testDB.runCommand({
aggregate: "article",
pipeline: [
{$project: {_id: 0, author: 1, commentsAuthor: "$comments.author"}},
{$sort: {author: 1}}
],
cursor: {}
});
let p9result = [
{"author": "bob", "commentsAuthor": ["joe", "sam"]},
{"author": "dave", "commentsAuthor": ["barbara", "jenny"]},
{"author": "jane", "commentsAuthor": ["will", "jenny"]}
];
assert.docEq(p9.cursor.firstBatch, p9result, 'p9 failed');
// simple sort
let p10 =
testDB.runCommand({aggregate: "article", pipeline: [{$sort: {title: 1}}], cursor: {}});
let p10result = [
{
"_id": 1,
"title": "this is my title",
"author": "bob",
"posted": ISODate("2004-03-21T18:59:54Z"),
"pageViews": 5,
"tags": ["fun", "good", "fun"],
"comments":
[{"author": "joe", "text": "this is cool"}, {"author": "sam", "text": "this is bad"}],
"other": {"foo": 5}
},
{
"_id": 3,
"title": "this is some other title",
"author": "jane",
"posted": ISODate("2000-12-31T05:17:14Z"),
"pageViews": 6,
"tags": ["nasty", "filthy"],
"comments": [
{"author": "will", "text": "i don't like the color"},
{"author": "jenny", "text": "can i get that in green?"}
],
"other": {"bar": 14}
},
{
"_id": 2,
"title": "this is your title",
"author": "dave",
"posted": ISODate("2030-08-08T04:11:10Z"),
"pageViews": 7,
"tags": ["fun", "nasty"],
"comments": [
{"author": "barbara", "text": "this is interesting"},
{"author": "jenny", "text": "i like to play pinball", "votes": 10}
],
"other": {"bar": 14}
}
];
assert.docEq(p10.cursor.firstBatch, p10result, 'p10 failed');
// unwind on nested array
testDB.p11.drop();
testDB.p11.save({
_id: 5,
name: 'MongoDB',
items: {authors: ['jay', 'vivek', 'bjornar'], dbg: [17, 42]},
favorites: ['pickles', 'ice cream', 'kettle chips']
});
let p11 = testDB.runCommand({
aggregate: "p11",
pipeline: [
{$unwind: "$items.authors"},
{$project: {name: 1, author: "$items.authors"}},
{$sort: {author: 1}}
],
cursor: {}
});
let p11result = [
{"_id": 5, "name": "MongoDB", "author": "bjornar"},
{"_id": 5, "name": "MongoDB", "author": "jay"},
{"_id": 5, "name": "MongoDB", "author": "vivek"},
];
assert.docEq(p11.cursor.firstBatch, p11result, 'p11 failed');
// multiply test
let p12 = testDB.runCommand({
aggregate: "article",
pipeline: [
{
$project: {
theProduct: {$multiply: ["$pageViews", {$ifNull: ["$other.foo", "$other.bar"]}]}
},
},
{$sort: {_id: 1}}
],
cursor: {}
});
let p12result =
[{"_id": 1, "theProduct": 25}, {"_id": 2, "theProduct": 98}, {"_id": 3, "theProduct": 84}];
assert.docEq(p12.cursor.firstBatch, p12result, 'p12 failed');
// subtraction test
let p13 = testDB.runCommand({
aggregate: "article",
pipeline: [
{
$project: {
theDifference:
{$subtract: ["$pageViews", {$ifNull: ["$other.foo", "$other.bar"]}]}
}
},
{$sort: {_id: 1}}
],
cursor: {}
});
let p13result = [
{"_id": 1, "theDifference": 0},
{"_id": 2, "theDifference": -7},
{"_id": 3, "theDifference": -8}
];
assert.docEq(p13.cursor.firstBatch, p13result, 'p13 failed');
// mod test
let p14 = testDB.runCommand({
aggregate: "article",
pipeline: [
{
$project: {
theRemainder: {
$mod: [
{$ifNull: ["$other.foo", "$other.bar"]},
"$pageViews",
]
}
}
},
{$sort: {_id: 1}}
],
cursor: {}
});
let p14result = [
{"_id": 1, "theRemainder": 0},
{"_id": 2, "theRemainder": 0},
{"_id": 3, "theRemainder": 2}
];
assert.docEq(p14.cursor.firstBatch, p14result, 'p14 failed');
// toUpper test
let p15 = testDB.runCommand({
aggregate: "article",
pipeline: [{$project: {author: {$toUpper: "$author"}, pageViews: 1}}, {$sort: {_id: 1}}],
cursor: {}
});
let p15result = [
{"_id": 1, "author": "BOB", "pageViews": 5},
{"_id": 2, "author": "DAVE", "pageViews": 7},
{"_id": 3, "author": "JANE", "pageViews": 6}
];
assert.docEq(p15.cursor.firstBatch, p15result, 'p15 failed');
// toLower test
let p16 = testDB.runCommand({
aggregate: "article",
pipeline: [
{$project: {author: {$toUpper: "$author"}, pageViews: 1}},
{$project: {author: {$toLower: "$author"}, pageViews: 1}},
{$sort: {_id: 1}}
],
cursor: {}
});
let p16result = [
{
"_id": 1,
"author": "bob",
"pageViews": 5,
},
{
"_id": 2,
"author": "dave",
"pageViews": 7,
},
{
"_id": 3,
"author": "jane",
"pageViews": 6,
}
];
assert.docEq(p16.cursor.firstBatch, p16result, 'p16 failed');
// substr test
let p17 = testDB.runCommand({
aggregate: "article",
pipeline: [
{
$project: {
author: {$substrBytes: ["$author", 1, 2]},
}
},
{$sort: {_id: 1}}
],
cursor: {}
});
let p17result =
[{"_id": 1, "author": "ob"}, {"_id": 2, "author": "av"}, {"_id": 3, "author": "an"}];
assert.docEq(p17.cursor.firstBatch, p17result, 'p17 failed');
// strcasecmp test
let p18 = testDB.runCommand({
aggregate: "article",
pipeline: [
{
$project: {
tags: 1,
thisisalametest: {$strcasecmp: ["foo", "bar"]},
thisisalamepass: {$strcasecmp: ["foo", "foo"]}
}
},
{$sort: {_id: 1}}
],
cursor: {}
});
let p18result = [
{"_id": 1, "tags": ["fun", "good", "fun"], "thisisalametest": 1, "thisisalamepass": 0},
{"_id": 2, "tags": ["fun", "nasty"], "thisisalametest": 1, "thisisalamepass": 0},
{"_id": 3, "tags": ["nasty", "filthy"], "thisisalametest": 1, "thisisalamepass": 0}
];
assert.docEq(p18.cursor.firstBatch, p18result, 'p18 failed');
// date tests
let p19 = testDB.runCommand({
aggregate: "article",
pipeline: [
{
$project: {
authors: 1,
posted: 1,
seconds: {$second: "$posted"},
minutes: {$minute: "$posted"},
hour: {$hour: "$posted"},
dayOfYear: {$dayOfYear: "$posted"},
dayOfMonth: {$dayOfMonth: "$posted"},
dayOfWeek: {$dayOfWeek: "$posted"},
month: {$month: "$posted"},
week: {$week: "$posted"},
year: {$year: "$posted"}
}
},
{$sort: {_id: 1}}
],
cursor: {}
});
let p19result = [
{
"_id": 1,
"posted": ISODate("2004-03-21T18:59:54Z"),
"seconds": 54,
"minutes": 59,
"hour": 18,
"dayOfYear": 81,
"dayOfMonth": 21,
"dayOfWeek": 1,
"month": 3,
"week": 12,
"year": 2004,
},
{
"_id": 2,
"posted": ISODate("2030-08-08T04:11:10Z"),
"seconds": 10,
"minutes": 11,
"hour": 4,
"dayOfYear": 220,
"dayOfMonth": 8,
"dayOfWeek": 5,
"month": 8,
"week": 31,
"year": 2030,
},
{
"_id": 3,
"posted": ISODate("2000-12-31T05:17:14Z"),
"seconds": 14,
"minutes": 17,
"hour": 5,
"dayOfYear": 366,
"dayOfMonth": 31,
"dayOfWeek": 1,
"month": 12,
"week": 53,
"year": 2000,
}
];
assert.docEq(p19.cursor.firstBatch, p19result, 'p19 failed');
testDB.lettype.drop();
testDB.lettype.save({x: 17, y: "foo"});
// ternary conditional operator
let p21 = testDB.runCommand({
aggregate: "article",
pipeline: [
{
$project: {
_id: 0,
author: 1,
pageViews: {
$cond: [
{$eq: ["$author", "dave"]},
{$add: ["$pageViews", 1000]},
"$pageViews"
]
}
}
},
{$sort: {author: 1}}
],
cursor: {}
});
let p21result = [
{"author": "bob", "pageViews": 5},
{"author": "dave", "pageViews": 1007},
{"author": "jane", "pageViews": 6}
];
assert.docEq(p21.cursor.firstBatch, p21result, 'p21 failed');
// simple matching
let m1 = testDB.runCommand(
{aggregate: "article", pipeline: [{$match: {author: "dave"}}], cursor: {}});
let m1result = [{
"_id": 2,
"title": "this is your title",
"author": "dave",
"posted": ISODate("2030-08-08T04:11:10Z"),
"pageViews": 7,
"tags": ["fun", "nasty"],
"comments": [
{"author": "barbara", "text": "this is interesting"},
{"author": "jenny", "text": "i like to play pinball", "votes": 10}
],
"other": {"bar": 14}
}];
assert.docEq(m1.cursor.firstBatch, m1result, 'm1 failed');
// combining matching with a projection
let m2 = testDB.runCommand({
aggregate: "article",
pipeline: [
{$project: {title: 1, author: 1, pageViews: 1, tags: 1, comments: 1}},
{$unwind: "$tags"},
{$match: {tags: "nasty"}},
{$sort: {_id: 1}}
],
cursor: {}
});
let m2result = [
{
"_id": 2,
"title": "this is your title",
"author": "dave",
"pageViews": 7,
"tags": "nasty",
"comments": [
{"author": "barbara", "text": "this is interesting"},
{"author": "jenny", "text": "i like to play pinball", "votes": 10}
]
},
{
"_id": 3,
"title": "this is some other title",
"author": "jane",
"pageViews": 6,
"tags": "nasty",
"comments": [
{"author": "will", "text": "i don't like the color"},
{"author": "jenny", "text": "can i get that in green?"}
]
}
];
assert.docEq(m2.cursor.firstBatch, m2result, 'm2 failed');
// group by tag, _id is a field reference
let g1 = testDB.runCommand({
aggregate: "article",
pipeline: [
{$project: {author: 1, tags: 1, pageViews: 1}},
{$unwind: "$tags"},
{$group: {_id: "$tags", docsByTag: {$sum: 1}, viewsByTag: {$sum: "$pageViews"}}},
{$sort: {'_id': 1}}
],
cursor: {}
});
let g1result = [
{"_id": "filthy", "docsByTag": 1, "viewsByTag": 6},
{"_id": "fun", "docsByTag": 3, "viewsByTag": 17},
{"_id": "good", "docsByTag": 1, "viewsByTag": 5},
{"_id": "nasty", "docsByTag": 2, "viewsByTag": 13},
];
assert.docEq(g1.cursor.firstBatch, g1result, 'g1 failed');
// $max, and averaging in a final projection; _id is structured
let g2 = testDB.runCommand({
aggregate: "article",
pipeline: [
{$project: {author: 1, tags: 1, pageViews: 1}},
{$unwind: "$tags"},
{
$group: {
_id: {tags: "$tags"},
docsByTag: {$sum: 1},
viewsByTag: {$sum: "$pageViews"},
mostViewsByTag: {$max: "$pageViews"},
}
},
{
$project: {
_id: false,
tag: "$_id.tags",
mostViewsByTag: 1,
docsByTag: 1,
viewsByTag: 1,
avgByTag: {$divide: ["$viewsByTag", "$docsByTag"]}
}
},
{$sort: {'docsByTag': 1, 'viewsByTag': 1}}
],
cursor: {}
});
let g2result = [
{"docsByTag": 1, "viewsByTag": 5, "mostViewsByTag": 5, "tag": "good", "avgByTag": 5},
{"docsByTag": 1, "viewsByTag": 6, "mostViewsByTag": 6, "tag": "filthy", "avgByTag": 6},
{"docsByTag": 2, "viewsByTag": 13, "mostViewsByTag": 7, "tag": "nasty", "avgByTag": 6.5},
{
"docsByTag": 3,
"viewsByTag": 17,
"mostViewsByTag": 7,
"tag": "fun",
"avgByTag": 5.666666666666667
}
];
assert.docEq(g2.cursor.firstBatch, g2result, 'g2 failed');
// $push as an accumulator; can pivot data
let g3 = testDB.runCommand({
aggregate: "article",
pipeline: [
{
$project: {
author: 1,
tags: 1,
}
},
{$unwind: "$tags"},
{$sort: {author: 1}},
{$group: {_id: {tags: "$tags"}, authors: {$push: "$author"}}},
{$sort: {'_id': 1}}
],
cursor: {}
});
let g3result = [
{"_id": {"tags": "filthy"}, "authors": ["jane"]},
{"_id": {"tags": "fun"}, "authors": ["bob", "bob", "dave"]},
{"_id": {"tags": "good"}, "authors": ["bob"]},
{"_id": {"tags": "nasty"}, "authors": ["dave", "jane"]}
];
assert.docEq(g3.cursor.firstBatch, g3result, 'g3 failed');
// $avg, and averaging in a final projection
let g4 = testDB.runCommand({
aggregate: "article",
pipeline: [
{$project: {author: 1, tags: 1, pageViews: 1}},
{$unwind: "$tags"},
{
$group: {
_id: {tags: "$tags"},
docsByTag: {$sum: 1},
viewsByTag: {$sum: "$pageViews"},
avgByTag: {$avg: "$pageViews"},
}
},
{$sort: {'_id': 1}}
],
cursor: {}
});
let g4result = [
{"_id": {"tags": "filthy"}, "docsByTag": 1, "viewsByTag": 6, "avgByTag": 6},
{"_id": {"tags": "fun"}, "docsByTag": 3, "viewsByTag": 17, "avgByTag": 5.666666666666667},
{"_id": {"tags": "good"}, "docsByTag": 1, "viewsByTag": 5, "avgByTag": 5},
{"_id": {"tags": "nasty"}, "docsByTag": 2, "viewsByTag": 13, "avgByTag": 6.5}
];
assert.docEq(g4.cursor.firstBatch, g4result, 'g4 failed');
// $addToSet as an accumulator; can pivot data
let g5 = testDB.runCommand({
aggregate: "article",
pipeline: [
{
$project: {
author: 1,
tags: 1,
}
},
{$unwind: "$tags"},
{$group: {_id: {tags: "$tags"}, authors: {$addToSet: "$author"}}},
{$sort: {'_id': 1}}
],
cursor: {}
});
// $addToSet doesn't guarantee order so we shouldn't test for it.
g5.cursor.firstBatch.forEach(function(obj) {
obj.authors.sort();
});
let g5result = [
{"_id": {"tags": "filthy"}, "authors": ["jane"]},
{
"_id": {"tags": "fun"},
"authors": [
"bob",
"dave",
]
},
{"_id": {"tags": "good"}, "authors": ["bob"]},
{
"_id": {"tags": "nasty"},
"authors": [
"dave",
"jane",
]
}
];
assert.docEq(g5.cursor.firstBatch, g5result, 'g5 failed');
// $first and $last accumulators, constant _id
let g6 = testDB.runCommand({
aggregate: "article",
pipeline: [
{$sort: {author: -1}},
{
$group: {
_id: "authors", /* constant string, *not* a field reference */
firstAuthor: {$last: "$author"}, /* note reverse sort above */
lastAuthor: {$first: "$author"}, /* note reverse sort above */
count: {$sum: 1}
}
}
],
cursor: {}
});
let g6result = [{"_id": "authors", firstAuthor: "bob", lastAuthor: "jane", count: 3}];
// Test unwind on an unused field
let g7 = testDB.runCommand({
aggregate: "article",
pipeline: [
{$unwind: '$tags'},
{
$group: {
_id: "tag_count", /* constant string, *not* a field reference */
count: {$sum: 1}
}
}
],
cursor: {}
});
assert.eq(g7.cursor.firstBatch[0].count, 7);
}());
|