mongo-go

How can I get JSON from BSON without my keys all being named “Key”?

匆匆过客 提交于 2019-12-25 19:37:53
问题 I'm trying to read from a database and then return the result to the user as json. What's happening is that I'm getting output like: [{"Key":"foo","Value":"bar"}] When I would like to get: "{"foo":"bar"}" How do I get the former? Example: (reading from the db and converting Raw to string are not shown) package main import ( "encoding/json" "fmt" "go.mongodb.org/mongo-driver/bson" ) func main() { var data = "{\"foo\":\"bar\"}" var testInterface interface{} e := bson.UnmarshalExtJSON([]byte

How can I get JSON from BSON without my keys all being named “Key”?

三世轮回 提交于 2019-12-25 19:37:15
问题 I'm trying to read from a database and then return the result to the user as json. What's happening is that I'm getting output like: [{"Key":"foo","Value":"bar"}] When I would like to get: "{"foo":"bar"}" How do I get the former? Example: (reading from the db and converting Raw to string are not shown) package main import ( "encoding/json" "fmt" "go.mongodb.org/mongo-driver/bson" ) func main() { var data = "{\"foo\":\"bar\"}" var testInterface interface{} e := bson.UnmarshalExtJSON([]byte

bulkwrite does not support multi-document transaction by using mongo-go-driver

自作多情 提交于 2019-12-25 01:09:11
问题 I's using mongo-go-driver 0.0.18 to build a bulk write which is consisted of a "NewUpdateManyModel" and several "NewInsertOneModel". My mongo server is atlas M10 with replica sets. I built some goroutines to test if the transactions are atomic, the result shows that each bulk write is not atomic, they would interfered with each other. I am wondering if mongo-go-driver supports for multi-document transaction? func insertUpdateQuery(counter int, col *mongo.Collection, group *sync.WaitGroup){

mongodb-go-driver/bson struct to bson.Document encoding

ぐ巨炮叔叔 提交于 2019-12-21 05:12:15
问题 I'm working with https://github.com/mongodb/mongo-go-driver and currently trying to implement a partial update of such struct type NoteUpdate struct { ID string `json:"id,omitempty" bson:"_id,omitempty"` Title string `json:"title" bson:"title,omitempty"` Content string `json:"content" bson:"content,omitempty"` ChangedAt int64 `json:"changed_at" bson:"changed_at"` } For instance, if I have noteUpdate := NoteUpdate{ Title: "New Title" } Then I expect that the only "title" field in the stored

How to ignore nulls while unmarshalling a MongoDB document?

馋奶兔 提交于 2019-12-20 02:56:38
问题 I would like to know if there's any approach that would allow me to ignore null types while unmarshalling a MongoDB document into a Go struct. Right now I have some auto-generate Go structs, something like this: type User struct { Name string `bson:"name"` Email string `bson:"email"` } Changing the types declared in this struct is not an option, and here's the problem; in a MongoDB database, which I do not have total control, some of the documents have been inserted with null values were

How to page a cursor in official mongo-go-driver

落爺英雄遲暮 提交于 2019-12-02 17:45:53
问题 Inspecting the interface Cursor in mongo-go-driver: https://github.com/mongodb/mongo-go-driver/blob/master/mongo/cursor.go#L37 There's no Limit or Skip functions. How can I page the results? I think I will face the same problem when trying to Sort or Count . Is there a way? or, is this simply not yet implemented in the official driver? 回答1: Most of the find options you can check in package options https://github.com/mongodb/mongo-go-driver/tree/master/mongo/options client, err := mongo

Watch for MongoDB Change Streams

最后都变了- 提交于 2019-11-29 11:47:33
We want our Go application to listen to the data changes on a collection. So, googling in search for a solution, we came across MongoDB's Change Streams . That link also exhibits some implementation snippets for a bunch of languages such as Python, Java, Nodejs etc. Yet, there is no piece of code for Go. We are using Mgo as a driver but could not find explicit statements on change streams . Does anyone have any idea on how to watch on Change Streams using that Mgo or any other Mongo driver for Go? The popular mgo driver ( github.com/go-mgo/mgo ) developed by Gustavo Niemeyer has gone dark

Watch for MongoDB Change Streams

前提是你 提交于 2019-11-28 05:30:58
问题 We want our Go application to listen to the data changes on a collection. So, googling in search for a solution, we came across MongoDB's Change Streams. That link also exhibits some implementation snippets for a bunch of languages such as Python, Java, Nodejs etc. Yet, there is no piece of code for Go. We are using Mgo as a driver but could not find explicit statements on change streams . Does anyone have any idea on how to watch on Change Streams using that Mgo or any other Mongo driver for