Golang 操作mongo

最近学习在go中操作mongodb,了解到主要有第三方mgo和官方mongo-driver两个库使用最多。mgo已经停止维护了,因此选择了mongo-driver。本文记录一些常用的代码操作笔记,以备随时查阅。

 

 

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

 

posted on 2022-04-01 17:13  星河赵  阅读(1155)  评论(0编辑  收藏  举报

导航