golang操作mongodb

mongodb 安装配置

vim /etc/mongod.conf
# 修改 net > bindip 为 0.0.0.0
# 重启,不然无法让其他网段的客户端连接

systemctl restart mongod
# 重启 mongod 后台进程

mongo shell 操作

mongodb 可以自动建表,不需要手动建

mongo localhost:27017
show dbs;
use my_db;

db.help();
# 查看版本阻挡
db.createCollection("my_collection")
show collections ;
# 相当于展示表名字
db.my_collection.insertOne({uid:1000,name:"helloWorld"})

> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB
my_db   0.000GB
> use my_db;
switched to db my_db
> show collections
my_collection
> db.my_collection.find();
{ "_id" : ObjectId("6371ffa38dd35a167d805a54"), "job_name" : "job", "command" : "ls", "err" : "", "content" : "hello", "time_point" : { "starttime" : ISODate("0001-01-01T00:00:00Z"), "endtime" : ISODate("0001-01-01T00:00:00Z") } }
>

golang 操作 mongodb

更新操作

package example

import (
	"context"
	"testing"
	"time"

	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type TimePoint struct {
	StartTime time.Time `json:"start_time,omitempty" form:"start_time" bsjon:"start_time"`
	EndTime   time.Time `json:"end_time,omitempty" form:"end_time" bsjon:"end_time"`
}
type LogRecord struct {
	JobName   string     `json:"job_name,omitempty" form:"job_name" bson:"job_name"`
	Command   string     `json:"command,omitempty" form:"command" bson:"command"`
	Err       string     `json:"err,omitempty" form:"err" bson:"err"`
	Content   string     `json:"content,omitempty" form:"content" bson:"content"`
	TimePoint *TimePoint `bson:"time_point"`
}

func Test_ExampleConnect(t *testing.T) {
	var (
		client *mongo.Client
		err    error
	)
	opt := options.Client().ApplyURI("mongodb://192.168.56.104:27017")
	// opt.ConnectTimeout(5 * time.Second)
	// opt2 := options.(5 * time.Second)
	client, err = mongo.Connect(context.TODO(), opt)
	if err != nil {
		t.Error(err)
		return
	}
	// ctx, _ := context.WithTimeout(context.TODO(), time.Second*4)
	// if err = client.Ping(ctx, nil); err != nil {
	// 	t.Error(err)
	// 	return
	// }
	database := client.Database("my_db")
	collection := database.Collection("my_collection")
	record := &LogRecord{
		JobName:   "job",
		Command:   "ls",
		Content:   "hello",
		TimePoint: &TimePoint{},
	}
	// 插入记录
	result, err := collection.InsertOne(context.TODO(), record)
	if err != nil {

		return
	}
	// 全局唯一ID
	t.Log(result)
	/*
		> db.my_collection.find();
			{ "_id" : ObjectId("6371ffa38dd35a167d805a54"), "job_name" : "job", "command" : "ls", "err" : "", "content" : "hello", "time_point" : { "starttime" : ISODate("0001-01-01T00:00:00Z"), "endtime" : ISODate("0001-01-01T00:00:00Z") } }
		>
	*/
}


查询操作

func Test_ExampleConnect2(t *testing.T) {
	type H = map[string]interface{}
	var (
		client *mongo.Client
		err    error
	)
	opt := options.Client().ApplyURI("mongodb://192.168.56.104:27017").SetTimeout(time.Second * 5)
	// opt.ConnectTimeout(5 * time.Second)
	// opt2 := options.(5 * time.Second)
	client, err = mongo.Connect(context.TODO(), opt)
	if err != nil {
		t.Error(err)
		return
	}
	database := client.Database("my_db")
	collection := database.Collection("my_collection")
	cursor, err := collection.Find(context.TODO(), struct{}{})
	if err != nil {
		t.Fatal(err)
	}
	//  这里可以自定义超时时间
	for cursor.Next(context.TODO()) {
		// 自定义结构体,为了方便,我用Map
		var mp H
		_ = cursor.Decode(&mp)
		t.Log(mp)
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值