基于golang官方mongo-driver操作总结
有如下json格式字符串,需要使用golang-mongo-driver,操作入库。
(该告警字符串来自WiseAPM告警中心)
写入过程是,将json格式字符串转化为字节类型,使用bson提供的UnmarshalExtJSON方法,构造一个bson.D数据结构,就可以使用Insert系列方法写入mongodb.
{
"_id" : ObjectId("5fedf5c834879b193cc0a649"),
"startTime" : NumberLong(1609430455),
"alertList" : [
{
"setId" : "s_548807257_2972083_56e5d1e6c60d6d4404f83bc61f312c8b",
"groupId" : "1",
"time" : NumberLong(1609430455),
"status" : "alert",
"setName" : "[ARGUS]ac82b78a-24df-43b7-85c4-6d49b6d6ee98::[DB_PG]SHE_primary_6262_pg6799yw",
"alertLevel" : "2",
"source" : "EMP-ALERT-ARGUS-INSTANCE",
"text" : "[P3 #1/3] pg_cpu_used all(#2) 95.23148>90",
"tags" : {
"alertDesc" : "pg_cpu_used",
"opcmObjType" : "Instance",
"note" : "",
"mail_s" : "",
"alertGrade" : "P3",
"OS" : "DB_POSTGRESQL_ADMIN",
"ip" : "",
"memo" : "",
"objName" : "ac82b78a-24df-43b7-85c4-6d49b6d6ee98::[DB_PG]SHE_primary_6262_pg6799yw",
"alertDepartment" : "",
"entity3" : "pg_cpu_used",
"mailFormatText" : "【告 警 状 态】告警\r\n【告 警 级 别】P3\r\n【告 警 对 象】ac82b78a-24df-43b7-85c4-6d49b6d6ee98::[DB_PG]SHE_primary_6262_pg6799yw\r\n【告 警 IP】\r\n【监 控 项】pg_cpu_used\r\n【监控 项 描述】pg_cpu_used\r\n【标 签】\r\n【备 注】\r\n【表 达 式】all(#2) >90\r\n【接收 人 分组】\r\n【最大告警次数】3\r\n【当前告警次数】1\r\n【事件触发时间】2021-01-01 00:00:55 +0800 GMT-8\r\n",
"entity1" : "0",
"maxAlertCount" : "3",
"mail_c" : "",
"opcmObjName" : "ac82b78a-24df-43b7-85c4-6d49b6d6ee98::[DB_PG]SHE_primary_6262_pg6799yw",
"currentAlertCount" : "1",
"linkUrl" : "",
"tag" : "",
"ukey" : "s_548807257_2972083_56e5d1e6c60d6d4404f83bc61f312c8b",
"objType" : "MiddleWare",
"alertDetailUrl" : "",
"environment" : "UNKNOW",
"status" : "UNKNOW"
},
"contact" : {},
"tagSet" : [
{
"tagk" : "alertDesc",
"tagv" : "pg_cpu_used"
},
{
"tagk" : "note",
"tagv" : ""
},
{
"tagk" : "mail_s",
"tagv" : ""
},
{
"tagk" : "alertGrade",
"tagv" : "P3"
},
{
"tagk" : "OS",
"tagv" : "DB_POSTGRESQL_ADMIN"
},
{
"tagk" : "ip",
"tagv" : ""
},
{
"tagk" : "memo",
"tagv" : ""
},
{
"tagk" : "objName",
"tagv" : "ac82b78a-24df-43b7-85c4-6d49b6d6ee98::[DB_PG]SHE_primary_6262_pg6799yw"
},
{
"tagk" : "alertDepartment",
"tagv" : ""
},
{
"tagk" : "entity3",
"tagv" : "pg_cpu_used"
},
{
"tagk" : "mailFormatText",
"tagv" : "【告 警 状 态】告警\r\n【告 警 级 别】P3\r\n【告 警 对 象】ac82b78a-24df-43b7-85c4-6d49b6d6ee98::[DB_PG]SHE_primary_6262_pg6799yw\r\n【告 警 IP】\r\n【监 控 项】pg_cpu_used\r\n【监控 项 描述】pg_cpu_used\r\n【标 签】\r\n【备 注】\r\n【表 达 式】all(#2) >90\r\n【接收 人 分组】\r\n【最大告警次数】3\r\n【当前告警次数】1\r\n【事件触发时间】2021-01-01 00:00:55 +0800 GMT-8\r\n"
},
{
"tagk" : "entity1",
"tagv" : "0"
},
{
"tagk" : "maxAlertCount",
"tagv" : "3"
},
{
"tagk" : "mail_c",
"tagv" : ""
},
{
"tagk" : "currentAlertCount",
"tagv" : "1"
},
{
"tagk" : "linkUrl",
"tagv" : ""
},
{
"tagk" : "tag",
"tagv" : ""
},
{
"tagk" : "ukey",
"tagv" : "s_548807257_2972083_56e5d1e6c60d6d4404f83bc61f312c8b"
},
{
"tagk" : "objType",
"tagv" : "MiddleWare"
},
{
"tagk" : "alertDetailUrl",
"tagv" : ""
},
{
"tagk" : "opcmObjName",
"tagv" : "ac82b78a-24df-43b7-85c4-6d49b6d6ee98::[DB_PG]SHE_primary_6262_pg6799yw"
},
{
"tagk" : "opcmObjType",
"tagv" : "Instance"
},
{
"tagk" : "name",
"tagv" : "[ARGUS]ac82b78a-24df-43b7-85c4-6d49b6d6ee98::[DB_PG]SHE_primary_6262_pg6799yw"
},
{
"tagk" : "source",
"tagv" : "EMP-ALERT-ARGUS-INSTANCE"
}
],
"alertIdSet" : [],
"metric" : "pg_cpu_used",
"lcd" : NumberLong(1609430456889),
"frequency" : 0.0,
"isCloseFlag" : "N",
"anom" : 0.0,
"step" : 0.0,
"argusClsoeTime" : 0.0
}
],
"endTime" : NumberLong(1609430485),
"preTime" : NumberLong(1606838455),
"eventName" : "[ARGUS]ac82b78a-24df-43b7-85c4-6d49b6d6ee98::[DB_PG]SHE_primary_6262_pg6799yw",
"nextTime" : NumberLong(1612022455),
"tags" : {
"alertDesc" : "pg_cpu_used",
"opcmObjType" : "Instance",
"note" : "",
"mail_s" : "",
"alertGrade" : "P3",
"OS" : "DB_POSTGRESQL_ADMIN",
"ip" : "",
"memo" : "",
"objName" : "ac82b78a-24df-43b7-85c4-6d49b6d6ee98::[DB_PG]SHE_primary_6262_pg6799yw",
"alertDepartment" : "",
"entity3" : "pg_cpu_used",
"mailFormatText" : "【告 警 状 态】告警\r\n【告 警 级 别】P3\r\n【告 警 对 象】ac82b78a-24df-43b7-85c4-6d49b6d6ee98::[DB_PG]SHE_primary_6262_pg6799yw\r\n【告 警 IP】\r\n【监 控 项】pg_cpu_used\r\n【监控 项 描述】pg_cpu_used\r\n【标 签】\r\n【备 注】\r\n【表 达 式】all(#2) >90\r\n【接收 人 分组】\r\n【最大告警次数】3\r\n【当前告警次数】1\r\n【事件触发时间】2021-01-01 00:00:55 +0800 GMT-8\r\n",
"entity1" : "0",
"maxAlertCount" : "3",
"mail_c" : "",
"opcmObjName" : "ac82b78a-24df-43b7-85c4-6d49b6d6ee98::[DB_PG]SHE_primary_6262_pg6799yw",
"currentAlertCount" : "1",
"linkUrl" : "",
"tag" : "",
"ukey" : "s_548807257_2972083_56e5d1e6c60d6d4404f83bc61f312c8b",
"objType" : "MiddleWare",
"alertDetailUrl" : "",
"environment" : "UNKNOW",
"status" : "UNKNOW"
},
"source" : "EMP-ALERT-ARGUS-INSTANCE",
"toolSource" : null,
"status" : "关闭",
"objName" : "ac82b78a-24df-43b7-85c4-6d49b6d6ee98::[DB_PG]SHE_primary_6262_pg6799yw",
"appName" : "",
"serviceName" : "",
"subsystemName" : "",
"buName" : "",
"group" : null,
"text" : "[P3 #1/3] pg_cpu_used all(#2) 95.23148>90",
"lcd" : NumberLong(1609430523522),
"metric" : "pg_cpu_used",
"step" : 0.0,
"department" : "",
"lastUpdateTime" : NumberLong(1609430523522),
"lastMergeTime" : NumberLong(1609430472034),
"mark" : false,
"setId" : "s_548807257_2972083_56e5d1e6c60d6d4404f83bc61f312c8b_1",
"tagSet" : [
{
"tagk" : "alertDesc",
"tagv" : "pg_cpu_used"
},
{
"tagk" : "note",
"tagv" : ""
},
{
"tagk" : "mail_s",
"tagv" : ""
},
{
"tagk" : "alertGrade",
"tagv" : "P3"
},
{
"tagk" : "OS",
"tagv" : "DB_POSTGRESQL_ADMIN"
},
{
"tagk" : "ip",
"tagv" : ""
},
{
"tagk" : "memo",
"tagv" : ""
},
{
"tagk" : "objName",
"tagv" : "ac82b78a-24df-43b7-85c4-6d49b6d6ee98::[DB_PG]SHE_primary_6262_pg6799yw"
},
{
"tagk" : "alertDepartment",
"tagv" : ""
},
{
"tagk" : "entity3",
"tagv" : "pg_cpu_used"
},
{
"tagk" : "mailFormatText",
"tagv" : "【告 警 状 态】告警\r\n【告 警 级 别】P3\r\n【告 警 对 象】ac82b78a-24df-43b7-85c4-6d49b6d6ee98::[DB_PG]SHE_primary_6262_pg6799yw\r\n【告 警 IP】\r\n【监 控 项】pg_cpu_used\r\n【监控 项 描述】pg_cpu_used\r\n【标 签】\r\n【备 注】\r\n【表 达 式】all(#2) >90\r\n【接收 人 分组】\r\n【最大告警次数】3\r\n【当前告警次数】1\r\n【事件触发时间】2021-01-01 00:00:55 +0800 GMT-8\r\n"
},
{
"tagk" : "entity1",
"tagv" : "0"
},
{
"tagk" : "maxAlertCount",
"tagv" : "3"
},
{
"tagk" : "mail_c",
"tagv" : ""
},
{
"tagk" : "currentAlertCount",
"tagv" : "1"
},
{
"tagk" : "linkUrl",
"tagv" : ""
},
{
"tagk" : "tag",
"tagv" : ""
},
{
"tagk" : "ukey",
"tagv" : "s_548807257_2972083_56e5d1e6c60d6d4404f83bc61f312c8b"
},
{
"tagk" : "objType",
"tagv" : "MiddleWare"
},
{
"tagk" : "alertDetailUrl",
"tagv" : ""
},
{
"tagk" : "opcmObjName",
"tagv" : "ac82b78a-24df-43b7-85c4-6d49b6d6ee98::[DB_PG]SHE_primary_6262_pg6799yw"
},
{
"tagk" : "opcmObjType",
"tagv" : "Instance"
},
{
"tagk" : "name",
"tagv" : "[ARGUS]ac82b78a-24df-43b7-85c4-6d49b6d6ee98::[DB_PG]SHE_primary_6262_pg6799yw"
},
{
"tagk" : "source",
"tagv" : "EMP-ALERT-ARGUS-INSTANCE"
}
],
"level" : "2",
"duration" : 30.0,
"num" : 1.0,
"createTime" : NumberLong(1609430472034),
"isArgus" : 1.0,
"alertInfo" : null,
"configType" : null,
"batchId" : null,
"eventHandleTime" : 30.0
}
首先,初始化mongodb连接池的代码:
common/mgo.go
package common
import (
"context"
"fmt"
"log"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type MongoDrivers struct {
Client *mongo.Client
Database string
}
type MongoCfg struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
Database string `yaml:"database"`
User string `yaml:"user"`
Password string `yaml:"password"`
}
var mongoClient *mongo.Client
var MgoDbName string
func GetMongoClient() *mongo.Client {
return mongoClient
}
func GetMongoDB() string {
return MgoDbName
}
// 初始化
func InitMongoDriver(m *MongoCfg) (err error) {
//设置客户端参数
//dsn := fmt.Sprintf("mongodb://%s:%s@%s:%d/%s", m.User, m.Password, m.Host, m.Port, m.Database)
dsn := fmt.Sprintf("mongodb://%s:%d", m.Host, m.Port)
clientOptions := options.Client().ApplyURI(dsn).
SetAuth(options.Credential{
AuthSource: m.Database,
Username: m.User,
Password: m.Password,
})
//连接到MongoDB
mongoClient, err = mongo.Connect(context.TODO(), clientOptions)
//defer client.Disconnect(context.TODO())
if err != nil {
log.Fatal(err)
return
}
//检查链接
err = mongoClient.Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
return
}
log.Println("[MongoDB] Connected to base MongoDB!")
return
}
// 关闭
func Close() {
err := mongoClient.Disconnect(context.TODO())
if err != nil {
log.Fatal(err)
}
log.Println("Connection to MongoDB closed.")
}
下入过程:
main.go:
import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
//mongoClient
mongoClient := common.GetMongoClient()
jsonBytes := []byte(jsonString)
var insert_bson_d = bson.D{}
err = bson.UnmarshalExtJSON(jsonBytes, true, &insert_bson_d)
_, err = mongoClient.Database(from_mongodb).Collection(to_collection).InsertOne(context.Background(), insert_bson_d)
同理,如果要将mongodb中复杂的数据结构,读出为json或map[string]interface{}格式,即可进行相反的操作过程(代码片段):
mongoClient := common.GetMongoClient()
findOptions := options.Find()
findOptions.SetSort(bson.D{{"_id", 1}})
filter := bson.D{{}}
cur, err := mongoClient.Database(from_mongodb).Collection(from_collection).Find(context.Background(), filter, findOptions)
var jsonMaps []map[string]interface{}
for cur.Next(context.Background()) {
var bsonDoc bson.D
var tmpBytes []byte
var jsonMap map[string]interface{}
err = cur.Decode(&bsonDoc)
if err != nil {
log.Fatal(err)
continue
}
tmpBytes, err = bson.MarshalExtJSON(bsonDoc, true, true)
if err != nil {
log.Fatal(err)
continue
}
err = json.Unmarshal(tmpBytes, &jsonMap)
if err != nil {
log.Fatal(err)
continue
}
jsonMaps = append(jsonMaps, jsonMap)
}