ljzsdut
GitHubToggle Dark/Light/Auto modeToggle Dark/Light/Auto modeToggle Dark/Light/Auto modeBack to homepage

Go语言操作mongodb

Install the MongoDB Go Driver

The MongoDB Go Driver is made up of several packages. If you are just using go get, you can install the driver using:

go get github.com/mongodb/mongo-go-driver
// go get github.com/mongodb/mongo-go-driver/mongo

Example code

package main

import (
    "context"
    //"encoding/json"
    "fmt"
    "github.com/mongodb/mongo-go-driver/bson"
    "github.com/mongodb/mongo-go-driver/mongo"
    "github.com/mongodb/mongo-go-driver/mongo/options"
    "log"
    "time"
)

func main() {
    // 连接数据库(方式1)
    ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)  // ctx
    opts := options.Client().ApplyURI("mongodb://localhost:27017")  // opts
    client, _ := mongo.Connect(ctx,opts)  // client
    
    // 使用NewClient链接(方式2)
   	// opts := options.Client().ApplyURI("mongodb://localhost:27017")  // opts
	// client, _ := mongo.NewClient(opts)  // client


    // 使用
    db := client.Database("mark")  // database
    stu := db.Collection("student") // collection
    
    // 插入数据   直接使用student struct实例
    xm := Student{Name:"小明",Age:17,Sex:M,}
    res, _ := stu.InsertOne(ctx,xm)
    
    // 查询数据  bson.D{}创建查询条件
    cur, err := stu.Find(ctx, bson.D{})  // find   
    CheckError(err)
    
    // 延时关闭游标
    defer cur.Close(ctx)
    for cur.Next(ctx) {
    
        // 可以decode到bson.M  也就是一个map[string]interface{}中
        // 也可以直接decode到一个对象中
        s := &Student{}
        var result bson.M
        err := cur.Decode(&result) // decode 到map
        err = cur.Decode(s)  // decode 到对象
        CheckError(err)
        
        // do something with result....
        // 可以将map 或对象序列化为json
        //js ,_:=json.Marshal(result)
        //json.Unmarshal(js,s) //反学序列化回来
        fmt.Println(s)
    }


}


type Gender uint8
const (
    M = iota
    F
)

type Student struct {
    Name string `json:"name"`
    Age int `json:"age"`
    Sex Gender `json:"gender"`
}



func CheckError( err error){
    if err != nil {
        log.Println(err.Error())
        return
    }
}

Use BSON Objects in Go

JSON documents in MongoDB are stored in a binary representation called BSON (Binary-encoded JSON). Unlike other databases that store JSON data as simple strings and numbers, the BSON encoding extends the JSON representation to include additional types such as int, long, date, floating point, and decimal128. This makes it much easier for applications to reliably process, sort, and compare data. The Go Driver has two families of types for representing BSON data: The D types and the Raw types.

The D family of types is used to concisely build BSON objects using native Go types. This can be particularly useful for constructing commands passed to MongoDB. The D family consists of four types:

  • D: A BSON document. This type should be used in situations where order matters, such as MongoDB commands.(适用于k-v顺序敏感的数据,例如MongoDB的命令,本质是一个slice:type D []E,即D是一个struct组成的slice) 补充:可以使用D.Map() 将D转换为M
  • M: An unordered map. It is the same as D, except it does not preserve order.(本质是一个map,无序:type M map[string]interface{}
  • A: A BSON array.(本质是一个slice:type A []interface{}
  • E: A single element inside a D.(本质是一个go原生的struct,表示一个k-v元素,该元素作为D的slice元素)-
type E struct {
	Key   string
	Value interface{}
}

Here is an example of a filter document built using D types which may be used to find documents where the name field matches either Alice or Bob:

bson.D{{
    "name", 
    bson.D{{
        "$in", 
        bson.A{"Alice", "Bob"}
    }}
}}


bson.D{
    {"foo", "bar"}, 
    {"hello", "world"}, 
    {"pi", 3.14159}
}

The Raw family of types is used for validating a slice of bytes. You can also retrieve single elements from Raw types using a Lookup(). This is useful if you don’t want the overhead of having to unmarshall the BSON into another type. This tutorial will just use the D family of types.

func main(){
    // bson.D 是一组key-value的集合  有序 
    d :=bson.D{
        {"Name","mark"},
        {"Age",12},
    }
    x :=d.Map() // 可以转为map
    fmt.Println(x)
    
    // bson.M是一个map 无序的key-value集合,在不要求顺序的情况下可以替代bson.D
    m :=bson.M{
        "name":"mark",
        "age":12,
    }
    fmt.Println(m)

    // bson.A 是一个数组
    a := bson.A{
        "jack","rose","jobs",
    }
    fmt.Println(a)

    // bson.E是只能包含一个key-value的map
    e :=bson.E{
        "name","mark",
    }
    fmt.Println(e)
}

Normal usages

Test connection is valid or not

    //pingerr :=client.Ping(ctx,nil)
    pingerr :=client.Ping(ctx,readpref.Primary())
    if pingerr != nil{
        fmt.Println(pingerr)
        return
    }

Get database or collection

c := client.Database("db_name").Collection("collection_name")

Drop collection

collection.Drop(ctx)

CRUD Operations

Insert Documents

insertOneRes, err := collection.InsertOne(ctx, data)
//InsertMany

Select Documents

// 查询单条数据
err := collection.FindOne(ctx, bson.D{{"name", "howie_2"}, {"age", 11}}).Decode(&obj)
// 查询单条数据后删除该数据
err = collection.FindOneAndDelete(ctx, bson.D{{"name", "howie_3"}})

// 询单条数据后修改该数据
err = collection.FindOneAndUpdate(ctx, bson.D{{"name", "howie_4"}}, bson.M{"$set": bson.M{"name": "这条数据我需要修改了"}})

// 查询单条数据后替换该数据
FindOneAndReplace(ctx, bson.D{{"name", "howie_5"}}, bson.M{"hero": "这条数据我替换了"})

// 一次查询多条数据(查询createtime>=3,限制取2条,createtime从大到小排序的数据)
cursor, err = collection.Find(ctx, bson.M{"createtime": bson.M{"$gte": 2}}, options.Find().SetLimit(2), options.Find().SetSort(bson.M{``"createtime"``: -1}))

// 查询数据数目g
Count

Update Document

// 修改一条数据
updateRes, err = collection.UpdateOne(ctx, bson.M{``"name"``: ``"howie_2"``}, bson.M{``"$set"``: bson.M{``"name"``: ``"我要改了他的名字"``}})
// 修改多条数据
updateRes, err = collection.UpdateMany(ctx, bson.M{``"createtime"``: bson.M{``"$gte"``: 3}}, bson.M{``"$set"``: bson.M{``"name"``: ``"我要批量改了他的名字"``}})

Delete Documents

// 删除1条
delRes, err = collection.DeleteOne(ctx, bson.M{``"name"``: ``"howie_1"``})
// 删除多条
delRes, err = collection.DeleteMany(getContext(), bson.M{``"createtime"``: bson.M{``"$gte"``: 7}})

示例:

package main

//导入
import (
	"context"
	"fmt"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"go.mongodb.org/mongo-driver/mongo/readpref"
	"os"
	"time"
)

type Howie struct {
	//struct里面获取ObjectID
	HowieId    primitive.ObjectID `bson:"_id"`
	Name       string
	Pwd        string
	Age        int64
	CreateTime int64
}

func main() {
	TestMongo("mongodb://192.168.5.183:8088")
}

func TestMongo(url string) {
	var (
		err             error
		client          *mongo.Client
		collection      *mongo.Collection
		insertOneRes    *mongo.InsertOneResult
		insertManyRes   *mongo.InsertManyResult
		delRes          *mongo.DeleteResult
		updateRes       *mongo.UpdateResult
		cursor          *mongo.Cursor
		howieArray      = GetHowieArray()
		howie           Howie
		howieArrayEmpty []Howie
		size            int64
	)
	//链接mongo服务
	if client, err = mongo.Connect(getContext(), options.Client().ApplyURI(url)); err != nil {
		checkErr(err)
	}
	//判断服务是否可用
	if err = client.Ping(getContext(), readpref.Primary()); err != nil {
		checkErr(err)
	}
	//选择数据库和集合
	collection = client.Database("testing_base").Collection("howie")

	//删除这个集合
	collection.Drop(getContext())

	//插入一条数据
	if insertOneRes, err = collection.InsertOne(getContext(), howieArray[0]); err != nil {
		checkErr(err)
	}

	fmt.Printf("InsertOne插入的消息ID:%v\n", insertOneRes.InsertedID)
	//批量插入数据
	if insertManyRes, err = collection.InsertMany(getContext(), howieArray[1:]); err != nil {
		checkErr(err)
	}
	fmt.Printf("InsertMany插入的消息ID:%v\n", insertManyRes.InsertedIDs)
	var Dinfo = make(map[string]interface{})
	err = collection.FindOne(getContext(), bson.D{{"name", "howie_2"}, {"age", 11}}).Decode(&Dinfo)
	fmt.Println(Dinfo)
	fmt.Println("_id", Dinfo["_id"])

	//查询单条数据
	// bson.D
	if err = collection.FindOne(getContext(), bson.D{{"name", "howie_2"}, {"age", 11}}).Decode(&howie); err != nil {
		checkErr(err)
	}
	fmt.Printf("FindOne查询到的数据:%v\n", howie)

	//查询单条数据后删除该数据
	if err = collection.FindOneAndDelete(getContext(), bson.D{{"name", "howie_3"}}).Decode(&howie); err != nil {
		checkErr(err)
	}
	fmt.Printf("FindOneAndDelete查询到的数据:%v\n", howie)

	//查询单条数据后修改该数据
	if err = collection.FindOneAndUpdate(getContext(), bson.D{{"name", "howie_4"}}, bson.M{"$set": bson.M{"name": "这条数据我需要修改了"}}).Decode(&howie); err != nil {
		checkErr(err)
	}
	fmt.Printf("FindOneAndUpdate查询到的数据:%v\n", howie)

	//查询单条数据后替换该数据(以前的数据全部清空)
	if err = collection.FindOneAndReplace(getContext(), bson.D{{"name", "howie_5"}}, bson.M{"hero": "这条数据我替换了"}).Decode(&howie); err != nil {
		checkErr(err)
	}

	fmt.Printf("FindOneAndReplace查询到的数据:%v\n", howie)
	//一次查询多条数据
	// 查询createtime>=3
	// 限制取2条
	// createtime从大到小排序的数据
	if cursor, err = collection.Find(getContext(), bson.M{"createtime": bson.M{"$gte": 2}}, options.Find().SetLimit(2), options.Find().SetSort(bson.M{"createtime": -1})); err != nil {
		checkErr(err)
	}
	if err = cursor.Err(); err != nil {
		checkErr(err)
	}
	defer cursor.Close(context.Background())
	for cursor.Next(context.Background()) {
		if err = cursor.Decode(&howie); err != nil {
			checkErr(err)
		}
		howieArrayEmpty = append(howieArrayEmpty, howie)
	}
	for _, v := range howieArrayEmpty {
		fmt.Printf("Find查询到的数据ObejectId值%s 值:%v\n", v.HowieId.Hex(), v)
	}
	//查询集合里面有多少数据
	if size, err = collection.CountDocuments(getContext(),bson.D{}); err != nil {
		checkErr(err)
	}
	fmt.Printf("Count里面有多少条数据:%d\n", size)

	//查询集合里面有多少数据(查询createtime>=3的数据)
	if size, err = collection.CountDocuments(getContext(), bson.M{"createtime": bson.M{"$gte": 3}}); err != nil {
		checkErr(err)
	}
	fmt.Printf("Count里面有多少条数据:%d\n", size)

	//修改一条数据
	if updateRes, err = collection.UpdateOne(getContext(), bson.M{"name": "howie_2"}, bson.M{"$set": bson.M{"name": "我要改了他的名字"}}); err != nil {
		checkErr(err)
	}
	fmt.Printf("UpdateOne的数据:%d\n", updateRes)

	//修改多条数据
	if updateRes, err = collection.UpdateMany(getContext(), bson.M{"createtime": bson.M{"$gte": 3}}, bson.M{"$set": bson.M{"name": "我要批量改了他的名字"}}); err != nil {
		checkErr(err)
	}

	fmt.Printf("UpdateMany的数据:%d\n", updateRes)
	//删除一条数据
	if delRes, err = collection.DeleteOne(getContext(), bson.M{"name": "howie_1"}); err != nil {
		checkErr(err)
	}
	fmt.Printf("DeleteOne删除了多少条数据:%d\n", delRes.DeletedCount)

	//删除多条数据
	if delRes, err = collection.DeleteMany(getContext(), bson.M{"createtime": bson.M{"$gte": 7}}); err != nil {
		checkErr(err)
	}
	fmt.Printf("DeleteMany删除了多少条数据:%d\n", delRes.DeletedCount)

}

func checkErr(err error) {
	if err != nil {
		if err == mongo.ErrNoDocuments {
			fmt.Println("没有查到数据")
			os.Exit(0)
		} else {
			fmt.Println(err)
			os.Exit(0)
		}
	}
}

func getContext() (ctx context.Context) {
	ctx, _ = context.WithTimeout(context.Background(), 10*time.Second)
	return
}

func GetHowieArray() (data []interface{}) {
	var i int64
	for i = 0; i <= 10; i++ {
		data = append(data, Howie{
			HowieId:    primitive.NewObjectID(),
			Name:       fmt.Sprintf("howie_%d", i+1),
			Pwd:        fmt.Sprintf("pwd_%d", i+1),
			Age:        i + 10,
			CreateTime: i + 1,
		})
	}
	return
}

构建objid:

primitive.ObjectIDFromHex(s string)
例如objID, err := primitive.ObjectIDFromHex("57eb654de4b0d82fb16e5329")