mgo

Go: how to run tests for multiple packages?

家住魔仙堡 提交于 2019-11-29 12:10:07
问题 I have multiple packages under a subdirectory under src/, running the tests for each package with go test is working fine. When trying to run all tests with go test ./... the tests are running but it fails.. the tests are running against local database servers, each test file has global variables with db pointers. I tried to run the tests with -parallel 1 to prevent contention in the db, but the tests still fail. what can be the issue here? EDIT: some tests are failing on missing DB entries,

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

Storing nested structs with mgo

吃可爱长大的小学妹 提交于 2019-11-29 11:37:05
问题 I'm trying to build a mongo document from a go struct that is heavily nested, and I'm running into a problem with the transition from go struct to a mongo object. I've built a very simplified version of what I'm trying to work with here: http://play.golang.org/p/yPZW88deOa package main import ( "os" "fmt" "encoding/json" ) type Square struct { Length int Width int } type Cube struct { Square Depth int } func main() { c := new(Cube) c.Length = 2 c.Width = 3 c.Depth = 4 b, err := json.Marshal(c

server returned error on SASL authentication step: Authentication failed

自闭症网瘾萝莉.ら 提交于 2019-11-28 16:14:07
问题 The following is my MongoDB connection dial from GoLang. But it's returning a panic " server returned error on SASL authentication step: Authentication failed. ". My username, password, hostAddrs and dbName are correct. What am I missing here? dbName: = os.Getenv("ENV_DBNAME") userName: = os.Getenv("ENV_DBUSER") password: = os.Getenv("ENV_DBPASS") dbHost: = os.Getenv("ENV_DBHOST") mongoDialInfo: = & mgo.DialInfo { Addrs: [] string { dbHost }, Database: dbName, Username: userName, Password:

How can I ask MongoDB to evaluate some JavaScript in order to obtain value for a field?

*爱你&永不变心* 提交于 2019-11-28 13:02:30
I want to let MongoDB dynamically assign a value to one of the fields of the document I'm inserting. For example: the current time from MongoDB server just like NOW() would do in MySQL. I tried this: c := mongoSession.DB("myapp").C("instances") rand.Seed(time.Now().UnixNano()) err := c.Insert( struct{Serial, Priority, Url, LastSeen interface{}}{ Url: getInformedHost() + ":" + getRunningPortString(), Priority: rand.Int(), LastSeen: mongoNow() } ) checkError(err, "Could not register on MongoDB server.", 3) I have this helper function: func mongoNow() bson.JavaScript { return bson.JavaScript{Code

How can I query MongoDB with date range using mgo and Go?

断了今生、忘了曾经 提交于 2019-11-28 09:50:15
Hi I have a collection named "my_sales" having fields product_name, price, sale_date. My doc looks like { "_id" : ObjectId("5458b6ee09d76eb7326df3a4"), "product_name" : product1, "price" : 200, "sale_date" : ISODate("2014-11-04T11:22:19.589Z") } { "_id" : ObjectId("5458b6ee09d76eb7326df3a4"), "product_name" : product1, "price" : 200, "sale_date" : ISODate("2014-11-04T11:22:19.589Z") } { "_id" : ObjectId("5458b6ee09d76eb7326df3a4"), "product_name" : product1, "price" : 200, "sale_date" : ISODate("2014-11-04T11:22:19.589Z") } { "_id" : ObjectId("5458b6ee09d76eb7326df3a4"), "product_name" :

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

Efficient paging in MongoDB using mgo

梦想的初衷 提交于 2019-11-27 20:53:06
I've searched and found no Go solution to the problem, not with or without using mgo.v2 , not on StackOverflow and not on any other site. This Q&A is in the spirit of knowledge sharing / documenting . Let's say we have a users collection in MongoDB modeled with this Go struct : type User struct { ID bson.ObjectId `bson:"_id"` Name string `bson:"name"` Country string `bson:"country"` } We want to sort and list users based on some criteria, but have paging implemented due to the expected long result list. To achieve paging of the results of some query, MongoDB and the mgo.v2 driver package has

Unstructured MongoDB collections with mgo

不羁的心 提交于 2019-11-27 18:49:30
I'm VERY new to Go. From what I've seen in the examples of mGo, in order to query a collection and then read from it, you have to predefine the data that will be coming back in a struct. type Person struct { ID bson.ObjectId `bson:"_id,omitempty"` Name string Phone string Timestamp time.Time } In PHP, the document was assigned to an array. This was perfect as one record may have completely different set of keys (may not contain Name or Phone but contain Email) and I could access it directly without setting up a predefined class / struct / variable. Is there a way to do the same in Go / mGo?

Concurrency in gopkg.in/mgo.v2 (Mongo, Go)

筅森魡賤 提交于 2019-11-27 07:44:08
问题 I wish to use MongoDB in webapp written in Go. Can I have one mgo.Session and use it concurrently in web app. e.g. in http.Handler or should I call Session.Copy and Session.Close -> make pool of sessions. It sounds contradictory somewhere I read that pool is already implemented inside of mgo.Session and I can use session concurrently and in other places I read that I need Copy and Close . 回答1: The mgo.Session is safe for concurrent use. Quoting from its doc: All Session methods are