res err userGamesCollection Distinct context Background gametype bson

 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
res, err := userGamesCollection.Distinct(context.Background(), "gametype", bson.M{})
fmt.Println(res)
fmt.Println(err)
for _, gametype := range res {
fmt.Println(gametype)
pipeline := []bson.D{
bson.D{{"$match", bson.D{{"gametype", "11"}}}},
bson.D{{
"$group", bson.M{
"_id": bson.M{
"date": bson.M{
"$dateToString": bson.M{
"format": "%Y-%m-%d",
"date": "$created",
},
},
"gametype": "$gametype",
},
"count": bson.M{"$sum": 1},
}}},
bson.D{{"$sort", bson.M{"_id": 1}}},
}
opts := options.Aggregate().SetMaxTime(5 * time.Minute)
cur, err := usersCollection.Aggregate(context.Background(), pipeline, opts)
defer cur.Close(ctx)
fmt.Println(cur)
fmt.Println(err)
var results []bson.M
cur.All(ctx, &results)
fmt.Println(results)
}