Monday 26 October 2020

go web app 9 mongodb delete

 
record deleted
//cmd
C:\Users\bob\golang3>go run main.go
deleted  1

//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)

deleteGrade, err := gradesCollection.DeleteOne(ctx, bson.M{"student_id": 123, "class_id": 456})
if err != nil {
log.Fatal(err)
}
fmt.Println("deleted ", deleteGrade.DeletedCount)
}

reference:

No comments:

Post a Comment