-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathaggregate_group_test.go
59 lines (52 loc) · 1.18 KB
/
aggregate_group_test.go
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
// Copyright 2018 Kuei-chun Chen. All rights reserved.
package examples
import (
"context"
"testing"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
/*
* count vehicles by style and display all brands and a total count of each style
*/
func TestAggregateGroup(t *testing.T) {
var err error
var client *mongo.Client
var collection *mongo.Collection
var cur *mongo.Cursor
var ctx = context.Background()
var doc bson.M
if client, err = getMongoClient(); err != nil {
t.Fatal(err)
}
defer client.Disconnect(ctx)
total := seedCarsData(client, dbName)
pipeline := `
[{
"$group": {
"_id": "$style",
"brand": {
"$addToSet": "$brand"
},
"count": {
"$sum": 1
}
}
}]`
collection = client.Database(dbName).Collection(collectionName)
opts := options.Aggregate()
if cur, err = collection.Aggregate(ctx, MongoPipeline(pipeline), opts); err != nil {
t.Fatal(err)
}
defer cur.Close(ctx)
count := int64(0)
for cur.Next(ctx) {
cur.Decode(&doc)
t.Log(doc["_id"], doc["count"])
count += toInt64(doc["count"])
}
if total != count {
t.Fatal("expected", total, "but got", count)
}
}