mgo

mgo time.Time or boolean check

萝らか妹 提交于 2019-12-23 02:17:38
问题 I have a mongo document which contains a date field which can also be false (or not defined), and I can't seem to find how to check if the field is available OR is false OR is a date (time.Time) in golang/mgo :S 回答1: If you have a time.Time field, and want to know whether it was properly set with a valid date, you can query its IsZero() method. Otherwise, if you're trying to query the database for such a document, you can do one of the following. Query if the field is false: iter :=

How to retrieve value from map - go lang?

半世苍凉 提交于 2019-12-22 11:19:55
问题 I am working with mgo to use MongoDB with Go. I have the following code: func Find(collectionName, dbName string, query interface{}) (result []interface{}, err error) { collection := session.DB(dbName).C(collectionName) err = collection.Find(query).All(&result) return result, err } func GetTransactionID() (id interface{}, err error) { query := bson.M{} transId, err := Find("transactionId", dbName, query) for key, value := range transId { fmt.Println("k:", key, "v:", value) } } Output: k: 0 v:

Get a value in a reference of a lookup with MongoDB and Golang

不问归期 提交于 2019-12-22 10:26:23
问题 I have the structures below. I use Golang 1.9.2 . // EventBoost describes the model of a EventBoost type EventBoost struct { ID string `bson:"_id" json:"_id" valid:"alphanum,printableascii"` CampaignID string `bson:"_campaign_id" json:"_campaign_id" valid:"alphanum,printableascii"` Name string `bson:"name" json:"name"` Description string `bson:"description" json:"description"` Level string `bson:"level" json:"level"` EventID string `bson:"_event_id" json:"_event_id" valid:"alphanum

Connections pool in Go mgo package

▼魔方 西西 提交于 2019-12-22 04:38:06
问题 In the article running-mongodb-queries-concurrently-with-go said that mgo.DialWithInfo : Create a session which maintains a pool of socket connections to MongoDB, but when I looking for in the documentacion of the function DialWithInfo I do not find something that talk me about pool connection, only I find something in the Dial Function Dial Function that said : This method is generally called just once for a given cluster. Further sessions to the same cluster are then established using the

How to construct an $or query in mgo

大憨熊 提交于 2019-12-21 03:51:23
问题 I am trying to convert this JS MongoDB query into Go mgo query: var foo = "bar"; db.collection.find({"$or": [ {uuid: foo}, {name: foo} ] }); This is what I've got so far, but it doesn't work: conditions := bson.M{"$or": []bson.M{bson.M{"uuid": name}, bson.M{"name": name}}} EDIT: It does seem to work now. Maybe I had a typo somewhere. 回答1: Here is a complete example which works fine for me (with Go 1.4, and MongoDB 2.6.5) package main import ( "fmt" "log" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2

Querying mongodb from golang using the _id stored in an array

限于喜欢 提交于 2019-12-18 09:13:27
问题 So here is my question. I have an array which are stored the _ids of mongodbs objects. Whats the right way to retrieve them all in one query using the mgo and bson package? So if the array is like that: ids:=["543d171c5b2c12420dd016","543d171c5b2dd016"] How we make the query ? I tried that but I know its wrong. query := bson.M{"_id": bson.M{"$in": ids}} c.Find(query).All() Thanks in advance 回答1: If the documents are stored with string ids, then the code looks correct. The ids look like hex

Connecting to MongoDB Atlas using Golang mgo: Persistent no reachable server to replica set

心不动则不痛 提交于 2019-12-18 02:43:21
问题 I have a replica set from mongodb atlas, to which I can connect with ANY other language, and regular mongo client, with the url provided with the format : "mongodb://user:pass@prefix1.mongodb.net:27017,prefix2.mongodb.net:27017,prefix3.mongodb.net:27017/test?&replicaSet=Cluster0-shard-0&authSource=admin" No matter what I tried, adding ssl=true and removing, nothing works. It is always "no reachable server". I tried every single combination for url, every combination for dialConfig, and also

Connecting to MongoDB Atlas using Golang mgo: Persistent no reachable server to replica set

两盒软妹~` 提交于 2019-12-18 02:43:08
问题 I have a replica set from mongodb atlas, to which I can connect with ANY other language, and regular mongo client, with the url provided with the format : "mongodb://user:pass@prefix1.mongodb.net:27017,prefix2.mongodb.net:27017,prefix3.mongodb.net:27017/test?&replicaSet=Cluster0-shard-0&authSource=admin" No matter what I tried, adding ssl=true and removing, nothing works. It is always "no reachable server". I tried every single combination for url, every combination for dialConfig, and also

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

我与影子孤独终老i 提交于 2019-12-17 20:41:33
问题 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

Golang mgo getting empty objects

二次信任 提交于 2019-12-17 14:58:07
问题 I'm trying to learn Go API development. I have a MongoDB instance running in a Docker container. I'm trying to follow a few guides but am failing on simple queries. I don't fully understand the use of BSON and JSON tags here. I do know what those terms mean. So here is my code. import ( "fmt" "time" "gopkg.in/mgo.v2/bson" ) const ( hosts = "localhost:27017" database = "my_database" username = "dev1" password = "password123" collection = "users" ) type users struct { user string `bson:"user"