连接
package mongodb
import (
"context"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
)
var mgoCli *mongo.Client
func initDb() {
var err error
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
//连接到MongoDB
mgoCli, err = mongo.Connect(context.TODO(), clientOptions)
if err != nil {
log.Fatal(err)
}
//检查连接
err = mgoCli.Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
}
}
func MgoCli() *mongo.Client {
if mgoCli == nil {
initDb()
}
return mgoCli
}
插入
package main
import (
"context"
"fmt"
"gitee.com/shirdonl/goWebActualCombat/chapter4/model"
"gitee.com/shirdonl/goWebActualCombat/chapter4/mongodb"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"time"
)
func main() {
var (
client = mongodb.MgoCli()
err error
collection *mongo.Collection
iResult *mongo.InsertOneResult
id primitive.ObjectID
)
//选择数据库 my_db里的某个表
collection = client.Database("my_db").Collection("my_collection")
//插入某一条数据
logRecord := model.LogRecord{
JobName: "job1",
Command: "echo 1",
Err: "",
Content: "1",
Tp: model.ExecTime{
StartTime: time.Now().Unix(),
EndTime: time.Now().Unix() + 10,
},
}
if iResult, err = collection.InsertOne(context.TODO(), logRecord); err != nil {
fmt.Print(err)
return
}
//_id:默认生成一个全局唯一ID
id = iResult.InsertedID.(primitive.ObjectID)
fmt.Println("自增ID", id.Hex())
//批量插入
result, err = collection.InsertMany(context.TODO(), []interface{}{
model.LogRecord{
JobName: "job multi1",
Command: "echo multi1",
Err: "",
Content: "1",
Tp: model.ExecTime{
StartTime: time.Now().Unix(),
EndTime: time.Now().Unix() + 10,
},
},
model.LogRecord{
JobName: "job multi2",
Command: "echo multi2",
Err: "",
Content: "2",
Tp: model.ExecTime{
StartTime: time.Now().Unix(),
EndTime: time.Now().Unix() + 10,
},
},
})
if err != nil{
log.Fatal(err)
}
if result == nil {
log.Fatal("result nil")
}
for _, v := range result.InsertedIDs {
id = v.(primitive.ObjectID)
fmt.Println("自增ID", id.Hex())
}
}
读取
package main
import (
"context"
"fmt"
"gitee.com/shirdonl/goWebActualCombat/chapter4/model"
"gitee.com/shirdonl/goWebActualCombat/chapter4/mongodb"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
)
func main() {
var (
client = mongodb.MgoCli()
err error
collection *mongo.Collection
cursor *mongo.Cursor
)
//选择数据库 my_db里的某个表
collection = client.Database("my_db").Collection("table1")
cond := model.FindByJobName{JobName: "job multi1"}
if cursor, err = collection.Find(
context.TODO(),
cond,
options.Find().SetSkip(0),
options.Find().SetLimit(2)); err != nil {
fmt.Println(err)
return
}
defer func() {
if err = cursor.Close(context.TODO()); err != nil {
log.Fatal(err)
}
}()
//遍历游标获取结果数据
for cursor.Next(context.TODO()) {
var lr model.LogRecord
//反序列化Bson到对象
if cursor.Decode(&lr) != nil {
fmt.Print(err)
return
}
fmt.Println(lr)
}
var results []model.LogRecord
if err = cursor.All(context.TODO(), &results); err != nil {
log.Fatal(err)
}
for _, result := range results {
fmt.Println(result)
}
}
|