草庐IT

bson-ext

全部标签

mongodb - 在 mongo-go-driver 中将 json 参数转换为 bson for UpdateOne

我正在尝试使用mongo-go-driver库的UpdateOne但此方法采用bson文档。我给它一个接口(interface)参数(json)。我的问题是找到将我的json请求解析为bson以动态更新字段的最佳方法。谢谢。func(sStore)Update(id`entercodehere`bson.D,dinterface{})(*mongo.UpdateResult,int32,string){upd:=bson.D{{"$inc",bson.D{d,},},}c,ctx,_:=getCollection(s.conn,s.dbName,s.collectionName)res

mongodb - MGO 驱动程序中的更新查询,适用于 bson.M,但不适用于自定义结构

Mgo和golang问题。我又遇到问题了。我尝试更新数据库中的记录,但运行简单命令visitors.UpdateId(v.Id,bson.M{"$set":zscore});wherezscore是类型Zscore的变量,不起作用。但是,如果我手动将zscore转换为bson.M结构,一切正常。有人知道如何使用mgo更新mongodb中的记录,而无需手动将结构值转储到bson.M中吗?示例:typeZscorestruct{afloat64`bson:"a,omitempty"json:"a"`bfloat64`bson:"b,omitempty"json:"b"`cfloat64`b

go - 如何在 "pretty"文本中查看mgo的bson.Raw

我希望能够将从golang的mgo库获取的原始bson数据输出到控制台以进行调试,但一直无法找到如何实现这一点。对于JSON,我是这样做的:formatedData,err:=json.MarshalIndent(rawData,"","")iferr!=nil{log.Print(err)}fmt.Printf("%s",formatedData)BSON是否有等效的方法? 最佳答案 bson是一种二进制格式,它只是字节的一部分。它本身是人类不可读的,因为这种格式包含有关字段长度等的信息,并且所有数据都非常紧凑。它已经编码,因此无

go - 将 map 转换为 bson

typePlayerstruct{idbson.ObjectIdtestmap[int]int}func(this*Player)Id()bson.ObjectId{returnthis.id}func(this*Player)DbObj()bson.D{testBson:=bson.D{}fork,v:=rangethis.test{testBson=append(testBson,bson.M{"id":k,"v":v})//compileerror}returnbson.D{{"_id",this.id},{"test",testBson},}}bson文档应该是:{'_id':

mongodb - 如何正确写嵌套的bson.M{}

假设我们有以下结构:typeshopstruct{IDprimitive.ObjectID`json:"_id,omitempty"bson:"_id,omitempty"`Brands*brand`json:"brand,omitempty"bson:"brand,omitempty"`}typebrandstruct{IDprimitive.ObjectID`json:"_id,omitempty"bson:"deploymentid,omitempty"`}我尝试使用findOne()查找文档,但即使使用MongoDBshell有匹配结果,我也没有得到任何文档。filter:=b

json - 解码 $oid 和 $date json/bson 去

我正在尝试在go中解码以下json字符串:{"dt":{"$date":1422019966844},"_id":{"$oid":"54c24d7eabb7c06d4f000371"}}我尝试了多种不同的方法来对此进行解码,但找不到有效的方法。将其解码为对象的惯用方法是什么?谢谢,Z. 最佳答案 如果您知道所获取的JSON的格式,那么最好的办法就是设计一个具有相同格式的结构。typeMyJSONstruct{Dtstruct{Dateint64`json:"$date"`}`json:"dt"`Idstruct{Oidstring`

Golang jsonapi 需要 string 或 int 但 mongo 需要 bson.ObjectId

使用go和以下包:github.com/julienschmidt/httproutergithub.com/shwoodard/jsonapigopkg.in/mgo.v2/bson我有以下结构:typeBlogstruct{Posts[]interface{}}typeBlogPoststruct{Idbson.ObjectId`jsonapi:"primary,posts"bson:"_id,omitempty"`Authorstring`jsonapi:"attr,author"`CreatedDatetime.Time`jsonapi:"attr,created_date"`

mongodb - 如何检索 []bson.M 类型的 map

如何检索多维[]bson.M类型的mapmongo中的数据是这样的"taskData":{"createdOn":ISODate("2016-02-20T21:23:11.903Z"),"Task_content":"@bob","Priority":"2","owner_Uname":"alice"}我试图访问它的代码varn[]bson.Me:=collection.Find(bson.M{"users."+strconv.Itoa(j)+".user_name":r.FormValue("value[userName]")}).Select(bson.M{"taskData.ow

json - MGO 返回 bson 字段而不是 json 字段

在mgo中执行pipe的时候会用到bson的名字。结构:typeTrainingstruct{Idbson.ObjectId`json:"id"bson:"_id"`Namestring`json:"name"bson:"name"`Descriptionstring`json:"description"`Level*TrainingLevel`json:"level"`Preworks[]bson.ObjectId`json:"preworks"`PrePostTests[]bson.ObjectId`json:"preposttests"bson:"preposttests"`Tr

mongodb - 如何正确使用 ObjectID 的 bson.MarshalJSON(myStruct)?

当我从我的数据库中抓取一个帖子并尝试将其呈现为JSON时,我遇到了一些问题:typePostBSONstruct{Idbson.ObjectId`bson:"_id,omitempty"`Titlestring`bson:"title"`}//...postBSON:=PostBSON{}id:=bson.ObjectIdHex(postJSON.Id)err=c.Find(bson.M{"_id":id}).One(&postBSON)//...response,err:=bson.MarshalJSON(postBSON)MarshalJSON不为我处理十六进制Id(ObjectI