demo1
package main
import (
"github.com/mongodb/mongo-go-driver/mongo"
"context"
"github.com/mongodb/mongo-go-driver/mongo/clientopt"
"time"
"fmt"
)
func main() {
var (
client *mongo.Client
err error
database *mongo.Database
collection *mongo.Collection
)
// 1, 建立连接
if client, err = mongo.Connect(context.TODO(), "mongodb://36.111.184.221:27017", clientopt.ConnectTimeout(5 * time.Second)); err != nil {
fmt.Println(err)
return
}
// 2, 选择数据库my_db
database = client.Database("my_db")
// 3, 选择表my_collection
collection = database.Collection("my_collection")
collection = collection
}
demo2
package main
import (
"github.com/mongodb/mongo-go-driver/mongo"
"context"
"github.com/mongodb/mongo-go-driver/mongo/clientopt"
"time"
"fmt"
"github.com/mongodb/mongo-go-driver/bson/objectid"
)
// 任务的执行时间点
type TimePoint struct {
StartTime int64 `bson:"startTime"`
EndTime int64 `bson:"endTime"`
}
// 一条日志
type LogRecord struct {
JobName string `bson:"jobName"` // 任务名
Command string `bson:"command"` // shell命令
Err string `bson:"err"` // 脚本错误
Content string `bson:"content"`// 脚本输出
TimePoint TimePoint `bson:"timePoint"`// 执行时间点
}
func main() {
var (
client *mongo.Client
err error
database *mongo.Database
collection *mongo.Collection
record *LogRecord
result *mongo.InsertOneResult
docId objectid.ObjectID
)
// 1, 建立连接
if client, err = mongo.Connect(context.TODO(), "mongodb://36.111.184.221:27017", clientopt.ConnectTimeout(5 * time.Second)); err != nil {
fmt.Println(err)
return
}
// 2, 选择数据库my_db
database = client.Database("cron")
// 3, 选择表my_collection
collection = database.Collection("log")
// 4, 插入记录(bson)
record = &LogRecord{
JobName: "job10",
Command: "echo hello",
Err: "",
Content: "hello",
TimePoint: TimePoint{StartTime: time.Now().Unix(), EndTime: time.Now().Unix() + 10},
}
if result, err = collection.InsertOne(context.TODO(), record); err != nil {
fmt.Println(err)
return
}
// _id: 默认生成一个全局唯一ID, ObjectID:12字节的二进制
docId = result.InsertedID.(objectid.ObjectID)
fmt.Println("自增ID:", docId.Hex())
}
demo3
package main
import (
"github.com/mongodb/mongo-go-driver/mongo"
"context"
"github.com/mongodb/mongo-go-driver/mongo/clientopt"
"time"
"fmt"
"github.com/mongodb/mongo-go-driver/bson/objectid"
)
// 任务的执行时间点
type TimePoint struct {
StartTime int64 `bson:"startTime"`
EndTime int64 `bson:"endTime"`
}
// 一条日志
type LogRecord struct {
JobName string `bson:"jobName"` // 任务名
Command string `bson:"command"` // shell命令
Err string `bson:"err"` // 脚本错误
Content string `bson:"content"`// 脚本输出
TimePoint TimePoint `bson:"timePoint"`// 执行时间点
}
func main() {
var (
client *mongo.Client
err error
database *mongo.Database
collection *mongo.Collection
record *LogRecord
logArr []interface{} // C语言里的addr, type, JAVA Object
result *mongo.InsertManyResult
insertId interface{} // objectId
docId objectid.ObjectID
)
// 1, 建立连接
if client, err = mongo.Connect(context.TODO(), "mongodb://36.111.184.221:27017", clientopt.ConnectTimeout(5 * time.Second)); err != nil {
fmt.Println(err)
return
}
// 2, 选择数据库my_db
database = client.Database("cron")
// 3, 选择表my_collection
collection = database.Collection("log")
// 4, 插入记录(bson)
record = &LogRecord{
JobName: "job10",
Command: "echo hello",
Err: "",
Content: "hello",
TimePoint: TimePoint{StartTime: time.Now().Unix(), EndTime: time.Now().Unix() + 10},
}
// 5, 批量插入多条document
logArr = []interface{}{record, record, record}
// 发起插入
if result, err = collection.InsertMany(context.TODO(), logArr); err != nil {
fmt.Println(err)
return
}
// 推特很早的时候开源的,tweet的ID
// snowflake: 毫秒/微秒的当前时间 + 机器的ID + 当前毫秒/微秒内的自增ID(每当毫秒变化了, 会重置成0,继续自增)
for _, insertId = range result.InsertedIDs {
// 拿着interface{}, 反射成objectID
docId = insertId.(objectid.ObjectID)
fmt.Println("自增ID:", docId.Hex())
}
}
demo4
package main
import (
"github.com/mongodb/mongo-go-driver/mongo"
"context"
"github.com/mongodb/mongo-go-driver/mongo/clientopt"
"time"
"fmt"
"github.com/mongodb/mongo-go-driver/mongo/findopt"
)
// 任务的执行时间点
type TimePoint struct {
StartTime int64 `bson:"startTime"`
EndTime int64 `bson:"endTime"`
}
// 一条日志
type LogRecord struct {
JobName string `bson:"jobName"` // 任务名
Command string `bson:"command"` // shell命令
Err string `bson:"err"` // 脚本错误
Content string `bson:"content"`// 脚本输出
TimePoint TimePoint `bson:"timePoint"`// 执行时间点
}
// jobName过滤条件
type FindByJobName struct {
JobName string `bson:"jobName"` // JobName赋值为job10
}
func main() {
// mongodb读取回来的是bson, 需要反序列为LogRecord对象
var (
client *mongo.Client
err error
database *mongo.Database
collection *mongo.Collection
cond *FindByJobName
cursor mongo.Cursor
record *LogRecord
)
// 1, 建立连接
if client, err = mongo.Connect(context.TODO(), "mongodb://36.111.184.221:27017", clientopt.ConnectTimeout(5 * time.Second)); err != nil {
fmt.Println(err)
return
}
// 2, 选择数据库my_db
database = client.Database("cron")
// 3, 选择表my_collection
collection = database.Collection("log")
// 4, 按照jobName字段过滤, 想找出jobName=job10, 找出5条
cond = &FindByJobName{JobName: "job10"} // {"jobName": "job10"}
// 5, 查询(过滤 +翻页参数)
if cursor, err = collection.Find(context.TODO(), cond, findopt.Skip(0), findopt.Limit(2)); err != nil {
fmt.Println(err)
return
}
// 延迟释放游标
defer cursor.Close(context.TODO())
// 6, 遍历结果集
for cursor.Next(context.TODO()) {
// 定义一个日志对象
record = &LogRecord{}
// 反序列化bson到对象
if err = cursor.Decode(record); err != nil {
fmt.Println(err)
return
}
// 把日志行打印出来
fmt.Println(*record)
}
}
demo5
package main
import (
"github.com/mongodb/mongo-go-driver/mongo"
"context"
"github.com/mongodb/mongo-go-driver/mongo/clientopt"
"time"
"fmt"
)
// startTime小于某时间
// {"$lt": timestamp}
type TimeBeforeCond struct {
Before int64 `bson:"$lt"`
}
// {"timePoint.startTime": {"$lt": timestamp} }
type DeleteCond struct {
beforeCond TimeBeforeCond `bson:"timePoint.startTime"`
}
func main() {
var (
client *mongo.Client
err error
database *mongo.Database
collection *mongo.Collection
delCond *DeleteCond
delResult *mongo.DeleteResult
)
// 1, 建立连接
if client, err = mongo.Connect(context.TODO(), "mongodb://36.111.184.221:27017", clientopt.ConnectTimeout(5 * time.Second)); err != nil {
fmt.Println(err)
return
}
// 2, 选择数据库my_db
database = client.Database("cron")
// 3, 选择表my_collection
collection = database.Collection("log")
// 4, 要删除开始时间早于当前时间的所有日志($lt是less than)
// delete({"timePoint.startTime": {"$lt": 当前时间}})
delCond = &DeleteCond{beforeCond: TimeBeforeCond{Before: time.Now().Unix()}}
// 执行删除
if delResult, err = collection.DeleteMany(context.TODO(), delCond); err != nil {
fmt.Println(err)
return
}
fmt.Println("删除的行数:", delResult.DeletedCount)
}