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