Thursday, 22 October 2020

go web app 5 mongodb retrive and query 2 filter sort

 //cmd - filter to find grade for student_id 0 in class_id 339

C:\Users\bob\golang3>go run main.go
[map[_id:ObjectID("56d5f7eb604eb380b0d8d8ce") class_id:339 scores:[map[score:78.40446309504266 type:exam] map[score:73.36224783231339 type:quiz] map[score:46.980982486720535 type:homework] map[score:76.67556138656222 type:homework]] student_id:0]]

//cmd - sort students in class_id 300 by descending order
C:\Users\bob\golang3>go run main.go
student ID:  9997
class ID: 300
grades:  [map[score:84.6558160678415 type:exam] map[score:41.03001163294812 type:quiz] map[score:76.39566038690292 type:homework] map[score:85.77890468455877 type:homework]]
student ID:  9975
class ID: 300
grades:  [map[score:8.252253589187186 type:exam] map[score:87.52808439331315 type:quiz] map[score:45.721796079306706 type:homework] map[score:27.86540394846814 type:homework]]
student ID:  9911
class ID: 300
grades:  [map[score:3.7139974722820157 type:exam] map[score:17.524760736805213 type:quiz] map[score:94.58247304193233 type:homework] map[score:11.05313964531678 type:homework]]
student ID:  9838
class ID: 300
grades:  [map[score:18.44047824370092 type:exam] map[score:40.223658996168865 type:quiz] map[score:62.644015337567836 type:homework] map[score:17.7905378078307 type:homework]]
student ID:  9733
class ID: 300
grades:  [map[score:77.00121051953168 type:exam] map[score:82.42737364488491 type:quiz] map[score:3.4209383539206994 type:homework] map[score:14.579084592497582 type:homework]]

//main.go

package main

import (
"context"
"fmt"
"log"
"time"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

func main() {
client, err := mongo.NewClient(options.Client().ApplyURI("mongodb+srv://bob:password@cluster0.yvyo2.mongodb.net/test?retryWrites=true&w=majority"))
if err != nil {
log.Fatal(err)
}
ctx, _ := context.WithTimeout(context.Background(), 100*time.Second)
err = client.Connect(ctx)
if err != nil {
log.Fatal(err)
}
defer client.Disconnect(ctx)

/*
err = client.Ping(ctx, readpref.Primary())
if err != nil {
log.Fatal(err)
}
databases, err := client.ListDatabaseNames(ctx, bson.M{})
err = client.Ping(ctx, readpref.Primary())
if err != nil {
log.Fatal(err)
}
fmt.Print(databases)
*/
trainingDatabase := client.Database("sample_training")
gradesCollection := trainingDatabase.Collection("grades")

cursor, err := gradesCollection.Find(ctx, bson.M{})
/*
if err != nil {
log.Fatal(err)
}
var grades []bson.M
if err = cursor.All(ctx, &grades); err != nil {
log.Fatal(err)
}
fmt.Println(grades)
*/
defer cursor.Close(ctx)

/*all
for i := 0; cursor.Next(ctx) && i < 5; i++ {
var grade bson.M
if err = cursor.Decode(&grade); err != nil {
log.Fatal(err)
}
fmt.Println("student ID: ", grade["student_id"])
fmt.Println("class ID:", grade["class_id"])
fmt.Println("grades: ", grade["scores"])
}
*/

/*filter
filterCursor, err := gradesCollection.Find(ctx, bson.M{"student_id": 0, "class_id": 339})
defer filterCursor.Close(ctx)
if err != nil {
log.Fatal(err)
}
var gradeFiltered []bson.M
if err = filterCursor.All(ctx, &gradeFiltered); err != nil {
log.Fatal(err)
}
fmt.Println(gradeFiltered)
*/

//sort
opts := options.Find()
opts.SetSort(bson.D{{"student_id", -1}})
sortCursor, err := gradesCollection.Find(ctx, bson.D{
{"class_id", bson.D{
{"$eq", 300},
}},
}, opts)
/*
var gradeSorted []bson.M
if err = sortCursor.All(ctx, &gradeSorted); err != nil {
log.Fatal(err)
}
fmt.Println(gradeSorted)
*/
for i := 0; sortCursor.Next(ctx) && i < 5; i++ {
var grade bson.M
if err = sortCursor.Decode(&grade); err != nil {
log.Fatal(err)
}
fmt.Println("student ID: ", grade["student_id"])
fmt.Println("class ID:", grade["class_id"])
fmt.Println("grades: ", grade["scores"])
}
}

reference:

No comments:

Post a Comment