-
Notifications
You must be signed in to change notification settings - Fork 35
/
main.go
90 lines (76 loc) · 2.85 KB
/
main.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
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
package main
import (
"context"
"fmt"
paginate "github.com/gobeam/mongo-go-pagination"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"time"
)
// Product struct
type Product struct {
Id primitive.ObjectID `json:"_id" bson:"_id"`
Name string `json:"name" bson:"name"`
Quantity float64 `json:"qty" bson:"qty"`
Price float64 `json:"price" bson:"price"`
}
func main() {
// Establishing mongo db connection
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
panic(err)
}
// Example for Normal Find query
filter := bson.M{}
var limit int64 = 10
var page int64 = 1
collection := client.Database("myaggregate").Collection("stocks")
projection := bson.D{
{"name", 1},
{"qty", 1},
}
// Querying paginated data
// If you want to do some complex sort like sort by score(weight) for full text search fields you can do it easily
// sortValue := bson.M{
// "$meta" : "textScore",
// }
// paginatedData, err := paginate.New(collection).Context(ctx).Limit(limit).Page(page).Sort("score", sortValue)...
var products []Product
paginatedData, err := paginate.New(collection).Context(ctx).Limit(limit).Page(page).Sort("price", -1).Sort("qty", -1).Select(projection).Filter(filter).Decode(&products).Find()
if err != nil {
panic(err)
}
// paginated data is in paginatedData.Data
// pagination info can be accessed in paginatedData.Pagination
//// if you want to marshal data to your defined struct
// print ProductList
fmt.Printf("Norm Find Data: %+v\n", products)
// print pagination data
fmt.Printf("Normal find pagination info: %+v\n", paginatedData.Pagination)
//Example for Aggregation
//match query
match := bson.M{"$match": bson.M{"qty": bson.M{"$gt": 10}}}
//group query
projectQuery := bson.M{"$project": bson.M{"_id": 1, "qty": 1}}
// you can easily chain function and pass multiple query like here we are passing match
// query and projection query as params in Aggregate function you cannot use filter with Aggregate
// because you can pass filters directly through Aggregate param
aggPaginatedData, err := paginate.New(collection).Context(ctx).Limit(limit).Page(page).Sort("price", -1).Aggregate(match, projectQuery)
if err != nil {
panic(err)
}
var aggProductList []Product
for _, raw := range aggPaginatedData.Data {
var product *Product
if marshallErr := bson.Unmarshal(raw, &product); marshallErr == nil {
aggProductList = append(aggProductList, *product)
}
}
// print ProductList
fmt.Printf("Aggregate Product List: %+v\n", aggProductList)
// print pagination data
fmt.Printf("Aggregate Pagination Data: %+v\n", aggPaginatedData.Pagination)
}