MongoDB介绍以及Go语言学习其API
mongoDB简介
MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。
MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。mongoDB存储的都是bjson数据,也就是二进制的json对象。
核心特性:
- 文档数据库,基于二进制json
- 高性能、高可用,可水平扩展
- 支持丰富的CRUD操作,例如:聚合统计、全文检索、坐标检索。
也可以参考如下两个链接学习:
http://www.runoob.com/mongodb/mongodb-tutorial.html
https://www.cnblogs.com/clsn/p/8214194.html
基本概念
MongoDB也有类似mysql的库表概念,可以类比如下:
mongoDB架构
整体架构:
- Mongod:单机版数据库
- Replica Set:复制集,又多个Mongod组成的高可用存储单元
- Sharding:分布式集群,由多个Replica Set组成
MongoD架构: - 默认采用Wired Tiger高性能存储引擎
- 基于journaling log 宕机恢复(类比mysql 的undo log)
Replica Set 架构: - 至少三个节点组成,其中一个可以只当arbiter
- 主从复制基于oplog(类似mysql 的 binlog)
- 客户端读写都默认通过primary节点
Sharding架构: - mongos 作为代理,路由请求到特定的shard
- 三个MongoD节点组成config server,保存元数据信息
- 每一个shard是一个Replica Set 可以无限扩容
Collection分片机制:
- collection自动分裂成多个chunk
- 每个chunk被自动负载均衡到不同的shard
- 每个shard可以保证其上的chunk高可用(因为有多个副本)
拆分chunk有两种方式:
Go语言API学习:
首先要下载好mongdb并启动:nohup bin/mongod --dbapth=./data --bind_ip=127.0.0.1 &
然后通过sdk调用相关API执行增删查改等操作
package main
import (
"go.mongodb.org/mongo-driver/mongo/options"
"time"
"go.mongodb.org/mongo-driver/mongo"
"context"
"fmt"
)
type LogRecord struct {
JobName string `bson:"jobNmae"`//任务名
Command string `bson:command`//shell命令
Err string `bson:err`//错误输出
Content string `bson:content`//脚本输出
TimePoint TimePoint `bson:"timePoint"`//时间信息
}
type TimePoint struct {
StartTime int64 `bson:"startTime"`
EndTime int64 `bson:"endTime"`
}
type JobNameFilter struct {
JobName string `bson:"jobNmae"`//任务名
}
type TimeFilter struct {
Before int64 `bson:"$lt"`
}
type DelCommand struct {
DelTime TimeFilter `bson:"timePoint.startTime"`
}
//学习使用mongodb
func main() {
var (
client *mongo.Client
database *mongo.Database
collection *mongo.Collection
curser *mongo.Cursor
delRest *mongo.DeleteResult
err error
)
con := context.TODO()
//1、建立连接
opt := options.Client()
opt.SetConnectTimeout(5*time.Second).ApplyURI("mongodb://127.0.0.1:27017")
if client,err = mongo.Connect(con,opt); err != nil {
fmt.Println(err)
return
}
//2、选择数据库
database = client.Database("cron")
//3、选择表my_collection
collection = database.Collection("log")
record := &LogRecord{
JobName: "job",
Command: "echo hello",
Err: "",
Content: "hello",
TimePoint: TimePoint{
StartTime: time.Now().Unix() - 10, EndTime: time.Now().Unix() + 10,
},
}
//插入一个
if insertRes, err := collection.InsertOne(con, record); err != nil {
fmt.Println(err)
return
} else {
fmt.Println(insertRes.InsertedID)
}
//插入多个
interMany := []interface{}{record,record}
if insertManyRes, err := collection.InsertMany(con,interMany);err != nil {
fmt.Println(err)
return
}else {
for _,v := range insertManyRes.InsertedIDs {
fmt.Println(v)
}
}
//查询操作
filter := &JobNameFilter{
JobName:"job",
}
limit := int64(2)
skip := int64(1)
findOpt := &options.FindOptions{
Limit:&limit,
Skip:&skip,
}
if curser,err = collection.Find(con,filter,findOpt);err != nil {
fmt.Println(err)
return
}
//释放游标
defer curser.Close(con)
result := &LogRecord{}
for curser.Next(con) {
curser.Decode(result)
fmt.Println("结果是:",*result)
}
//删除操作:删除创建时间早于当前时间的日志
delCommand := &DelCommand{
DelTime:TimeFilter{
Before:time.Now().Unix(),
},
}
if delRest,err = collection.DeleteMany(con,delCommand);err != nil {
fmt.Println(err)
return
}
fmt.Println("删除了:",delRest.DeletedCount,"条")
return
}