Golang 操作mongo
阿新 • • 發佈:2022-04-01
最近學習在go中操作mongodb,瞭解到主要有第三方mgo和官方mongo-driver兩個庫使用最多。mgo已經停止維護了,因此選擇了mongo-driver。本文記錄一些常用的程式碼操作筆記,以備隨時查閱。
```go package main
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
"reflect"
)
// Trainer type is used for later
type Trainer struct {
Name string
Age int
City string
}
func main() {
// Set client options
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
// Connect to MongoDB client, err := mongo.Connect(context.TODO(), clientOptions) if err != nil { log.Fatal(err) } // Check the connection err = client.Ping(context.TODO(), nil) if err != nil { log.Fatal(err) } fmt.Println("Connected to MongoDB!") // Get collection collection := client.Database("test").Collection("trainers") ash := Trainer{"Ash", 10, "Pallet Town11"} misty := Trainer{"misty", 10, "Cerulean City"} brock := Trainer{"Brock", 15, "Pewter City"} // Insert insertResult, err := collection.InsertOne(context.TODO(), ash) if err != nil { log.Fatal(err) } fmt.Println("Inserted a single document: ", insertResult.InsertedID) // InsertMany trainers := []interface{}{misty, brock} insertManyResult, err := collection.InsertMany(context.TODO(), trainers) if err != nil { log.Fatal(err) } fmt.Println("Inserted multiple documents: ", insertManyResult.InsertedIDs) // Update filter := bson.D{{"name", "Ash"}} fmt.Println("filter:", reflect.TypeOf(filter)) update := bson.D{ {"$inc", bson.D{ {"age", 1}, }}, } fmt.Println("update:", reflect.TypeOf(update)) updateResult, err := collection.UpdateOne(context.TODO(), filter, update) if err != nil { log.Fatal(err) } fmt.Printf("Matched %v documents and updated %v documents.n", updateResult.MatchedCount, updateResult.ModifiedCount) // Search var result Trainer err = collection.FindOne(context.TODO(), filter).Decode(&result) if err != nil { log.Fatal(err) } fmt.Printf("Found a single document: %+vn", result) findOptions := options.Find() findOptions.SetLimit(5) var results []*Trainer cur, err := collection.Find(context.TODO(), bson.D{{}}, findOptions) if err != nil { log.Fatal(err) } for cur.Next(context.TODO()) { var elem Trainer err := cur.Decode(&elem) if err != nil { log.Fatal(err) } results = append(results, &elem) } if err := cur.Err(); err != nil { log.Fatal(err) } cur.Close(context.TODO()) fmt.Printf("Found multiple documents (array of pointers): %+vn", results) // DeleteMany deleteResult, err := collection.DeleteMany(context.TODO(), bson.D{{}}) if err != nil { log.Fatal(err) } fmt.Printf("Deleted %v documents in the trainers collectionn", deleteResult.DeletedCount)
}