Skip to content

Commit

Permalink
example upgrade completed
Browse files Browse the repository at this point in the history
  • Loading branch information
gobeam committed Jun 21, 2021
1 parent d16b35a commit fd27a35
Showing 1 changed file with 10 additions and 76 deletions.
86 changes: 10 additions & 76 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
type Product struct {
Id primitive.ObjectID `json:"_id,omitempty" bson:"_id"`
Name string `json:"name,omitempty" bson:"name"`
Quantity float64 `json:"qty,omitempty" bson:"qty"`
Quantity float64 `json:"quantity,omitempty" bson:"quantity"`
Price float64 `json:"price,omitempty" bson:"price"`
}

Expand All @@ -28,7 +28,7 @@ func insertExamples(db *mongo.Database) (insertedIds []interface{}, err error) {
data = append(data, bson.M{
"name": fmt.Sprintf("product-%d", i),
"quantity": float64(i),
"Price": float64(i*10+5),
"price": float64(i*10+5),
})
}
result, err := db.Collection("products").InsertMany(
Expand All @@ -49,76 +49,10 @@ func main() {
panic(err)
}
dbConnection = client.Database("myaggregate")
////_, insertErr := insertExamples(client.Database("myaggregate"))
////if insertErr != nil {
//// panic(insertErr)
////}
//
//// Example for Normal Find query
//filter := bson.M{}
//var limit int64 = 10
//var page int64 = 2
//collection := client.Database("myaggregate").Collection("products")
//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)
//}
//fmt.Println(paginatedData)
//res, _ := json.Marshal(paginatedData)
//fmt.Println(string(res))
//// 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)



_, insertErr := insertExamples(client.Database("myaggregate"))
if insertErr != nil {
panic(insertErr)
}

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Testing pagination go to http://localhost:8081/normal-pagination?page=1&limit=10 to beign testing")
Expand All @@ -133,7 +67,7 @@ func main() {
collection := dbConnection.Collection("products")
projection := bson.D{
{"name", 1},
{"qty", 1},
{"quantity", 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
Expand All @@ -142,7 +76,7 @@ func main() {
// }
// 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()
paginatedData, err := paginate.New(collection).Context(ctx).Limit(limit).Page(page).Sort("price", -1).Sort("quantity", -1).Select(projection).Filter(filter).Decode(&products).Find()
if err != nil {
panic(err)
}
Expand All @@ -167,10 +101,10 @@ func main() {
limit := int64(convertedLimitInt)
page := int64(convertedPageInt)
//match query
match := bson.M{"$match": bson.M{"price": bson.M{"$gt": 0}}}
match := bson.M{"$match": bson.M{"quantity": bson.M{"$gt": 0}}}
//
//group query
projectQuery := bson.M{"$project": bson.M{"_id": 1, "name": 1, "qty": 1}}
projectQuery := bson.M{"$project": bson.M{"_id": 1, "name": 1, "quantity": 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
Expand Down

0 comments on commit fd27a35

Please sign in to comment.