mongodb中BSON怎么使用?BSON使用示例详解
Admin 2021-05-15 群英技术资讯 1521 次浏览
BSON是一个数据格式,主要用于mongodb中,也是mongodb中最基础的数据存储格式,文本就主要给大家介绍mongodb BSON格式的基本使用,感兴趣的朋友就继续往下看吧。
查找 Find
m := bson.M{
"create_time": bson.M{
"$gte": start,
"$lte": end,
},
"account": account,
"tag": "tag",
}
session.DB("db").C("collect").Find(m).Count()
这里查找时间戳内,账号为account,标签为tag的数据并统计个数。
聚合管道在mgo中为Pipe(pipeline interface{})
这个和bash中使用的管道很像,数据可以被层层处理。一般传入的参数为[]bson.M。这个[]bson.M里如果还有嵌套则还要使用[]bson.M
- 比如这里首先匹配标签和账号
- 时间戳在一段时间内
- 然后根据名字分组统计数量
- 最后排序取最前面的三个。
//这个就可以传入Pipe
m := []bson.M{
{"$match": bson.M{"tag": "tag", "account": account, "create_time": bson.M{"$gte": start, "$lte": end}}},
{"$group": bson.M{"_id": "$TagName", "count": bson.M{"$sum": 1}}},
{"$sort": bson.M{"count": -1}},
{"$limit": 3},
}
//这里就可以取到输出的数据
var values []result
session.DB("db").C("collect").Pipe(m).All(&values)
简单介绍
package main
import (
"gopkg.in/mgo.v2"
"log"
"gopkg.in/mgo.v2/bson"
)
type User struct {
Id bson.ObjectId `bson:"_id"`
Name string `bson:"name"`
PassWord string `bson:"pass_word"`
Age int `bson:"age"`
}
func main() {
db, err := mgo.Dial("mongodb://192.168.2.28:27017,192.168.2.28:27018,192.168.2.28:27019/?replicaSet=howie")
if err != nil {
log.Fatalln(err)
}
defer db.Close()
db.SetMode(mgo.Monotonic, true)
c := db.DB("howie").C("person")
//插入
/*c.Insert(&User{
Id: bson.NewObjectId(),
Name: "JK_CHENG",
PassWord: "123132",
Age: 2,
}, &User{
Id: bson.NewObjectId(),
Name: "JK_WEI",
PassWord: "qwer",
Age: 5,
}, &User{
Id: bson.NewObjectId(),
Name: "JK_HE",
PassWord: "6666",
Age: 7,
})*/
var users []User
c.Find(nil).All(&users) //查询全部数据
log.Println(users)
c.FindId(users[0].Id).All(&users) //通过ID查询
log.Println(users)
c.Find(bson.M{"name": "JK_WEI"}).All(&users) //单条件查询(=)
log.Println(users)
c.Find(bson.M{"name": bson.M{"$ne": "JK_WEI"}}).All(&users) //单条件查询(!=)
log.Println(users)
c.Find(bson.M{"age": bson.M{"$gt": 5}}).All(&users) //单条件查询(>)
log.Println(users)
c.Find(bson.M{"age": bson.M{"$gte": 5}}).All(&users) //单条件查询(>=)
log.Println(users)
c.Find(bson.M{"age": bson.M{"$lt": 5}}).All(&users) //单条件查询(<)
log.Println(users)
c.Find(bson.M{"age": bson.M{"$lte": 5}}).All(&users) //单条件查询(<=)
log.Println(users)
/*c.Find(bson.M{"name": bson.M{"$in": []string{"JK_WEI", "JK_HE"}}}).All(&users) //单条件查询(in)
log.Println(users)
c.Find(bson.M{"$or": []bson.M{bson.M{"name": "JK_WEI"}, bson.M{"age": 7}}}).All(&users) //多条件查询(or)
log.Println(users)
c.Update(bson.M{"_id": users[0].Id}, bson.M{"$set": bson.M{"name": "JK_HOWIE", "age": 61}}) //修改字段的值($set)
c.FindId(users[0].Id).All(&users)
log.Println(users)
c.Find(bson.M{"name": "JK_CHENG", "age": 66}).All(&users) //多条件查询(and)
log.Println(users)
c.Update(bson.M{"_id": users[0].Id}, bson.M{"$inc": bson.M{"age": -6,}}) //字段增加值($inc)
c.FindId(users[0].Id).All(&users)
log.Println(users)*/
//c.Update(bson.M{"_id": users[0].Id}, bson.M{"$push": bson.M{"interests": "PHP"}}) //从数组中增加一个元素($push)
c.Update(bson.M{"_id": users[0].Id}, bson.M{"$pull": bson.M{"interests": "PHP"}}) //从数组中删除一个元素($pull)
c.FindId(users[0].Id).All(&users)
log.Println(users)
c.Remove(bson.M{"name": "JK_CHENG"})//删除
}
总结
现在大家对于mongodb中bson格式的基本用法应该都有一定的了解了,希望文本对大家学习有帮助,想要了解更多mongodb中bson格式的内容大家可以继续关注其他相关文章。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
在MongoDB中使用sort()方法对数据进行排序,sort()方法可以通过参数指定排序的字段,并使用1和-1来指定排序的方式,其中1为升序排列,而-1是用于降序排列。
MongoDB监控工具有哪些?在MongoDB中,有自带两个监控的工具,分别是mongostat和mongotop,很多刚接触MongoDB的朋友可能对于这两个工具的使用不是很了解,对此这篇文章就主要给大家介绍一下关于mongostat和mongotop的使用,感兴趣的朋友可以了解一下。
TTL索引用于什么,怎样使用的呢?有不少朋友对此感兴趣,下面小编给大家整理和分享了相关知识和资料,易于大家学习和理解,有需要的朋友可以借鉴参考,下面我们一起来了解一下吧。
判断mongodb是否启动的方法:可以通过查看端口27017的状态来判断。在终端执行【netstat -lanp | grep "27017"】命令即可。如果未开启,可以通过执行【service mongodb start】命令来开启。
在MongoDB中时间存储方式:1、将时间戳存为Number格式。2、使用MongoDB自带的日期格式存储。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008