加入收藏 | 设为首页 | 会员中心 | 我要投稿 银川站长网 (https://www.0951zz.com/)- 云通信、基础存储、云上网络、机器学习、视觉智能!
当前位置: 首页 > 服务器 > 系统 > 正文

MongoDB中使用 BSON 的详细案例分析

发布时间:2023-09-26 13:13:27 所属栏目:系统 来源:
导读: BSON是一个数据格式,主要用于mongodb中,也是mongodb中最基础的数据存储格式,文本就主要给大家介绍mongodb BSON格式的基本使用,感兴趣的朋友就继续往下看吧。 查找 Findm := bson.M{"create_time": bson.M

    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格式的内容大家可以继续关注其他相关文章。

(编辑:银川站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章