MongoDB介绍

MongoDB是目前比较流行的一个基于分布式文件存储的数据库,它是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库中功能最丰富,最像关系数据库的
MongoDB中将一条数据储存为一个文档(document),数据结构由键值对(K-V)组成,其中文档类似于平常使用到的JSON对象,文档中的字段值可以包含其他文档,数组,及文档数组

insert

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

func main() {
	ctx := context.Background()
	mc, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://username:password@localhost:27017/?authSource=admin"))
	if err != nil {
		panic(err)
	}

	col := mc.Database("coolcar").Collection("account")
	insertRows(col, ctx)
}

// insertRows 新增mongodb
func insertRows(col *mongo.Collection, ctx context.Context) {
	res, err := col.InsertMany(ctx, []interface{}{
		bson.M{
			"open_id": "123",
		},
		bson.M{
			"open_id": "456",
		},
	})
	if err != nil {
		panic(err)
	}
	// &{InsertedIDs:[ObjectID("623f086deda4096b61beaae4") ObjectID("623f086deda4096b61beaae5")]}
	fmt.Printf("%+v", res)
}

在这里插入图片描述

delete

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

func main() {
	ctx := context.Background()
	mc, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://username:password@localhost:27017/?authSource=admin"))
	if err != nil {
		panic(err)
	}

	col := mc.Database("coolcar").Collection("account")
	deleteRows(col, ctx)
}

// deleteRows 输出操作的条数
func deleteRows(col *mongo.Collection, ctx context.Context) {
	res, err := col.DeleteOne(ctx, bson.M{"open_id": "456"})
	if err != nil {
		panic(err)
	}
	// 1
	fmt.Println(res.DeletedCount)
}

在这里插入图片描述

update

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

func main() {
	ctx := context.Background()
	mc, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://username:password@localhost:27017/?authSource=admin"))
	if err != nil {
		panic(err)
	}

	col := mc.Database("coolcar").Collection("account")
	updateRows(col, ctx)
}

// updateRows 输出操作的条数
func updateRows(col *mongo.Collection, ctx context.Context) {
	res, err := col.UpdateOne(ctx, bson.M{"open_id": "123"}, bson.M{"$set": bson.M{"open_id": "123456"}})
	if err != nil {
		panic(err)
	}
	// 1
	fmt.Println(res.ModifiedCount)
}

在这里插入图片描述

find

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

func main() {
	ctx := context.Background()
	mc, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://username:password@localhost:27017/?authSource=admin"))
	if err != nil {
		panic(err)
	}

	col := mc.Database("coolcar").Collection("account")
	findRows(ctx, col)
}

// findRows 查询mongo
func findRows(ctx context.Context, col *mongo.Collection) {
	res := col.FindOne(ctx, bson.M{
		"open_id": "123456",
	})
	var row struct {
		Id     primitive.ObjectID `bson:"_id"`
		OpenID string             `bson:"open_id"`
	}
	err := res.Decode(&row)
	if err != nil {
		panic(err)
	}
	// {Id:ObjectID("623f086deda4096b61beaae4") OpenID:123456}
	fmt.Printf("%+v\n", row)
}
Logo

华为云1024程序员节送福利,参与活动赢单人4000元礼包,更有热门技术干货免费学习

更多推荐