mgo

Golang mgo Finding

血红的双手。 提交于 2019-12-25 00:05:39
问题 I try to find my user in MongoDB but when I run this code : type Person struct { Id bson.ObjectId `bson:"_id,omitempty"`//`json:"id" bson:"_id,omitempty"` username string `json:"username" bson:"username"` score string `json:"score" bson:"score"` level string `json:"level" bson:"level"` } result := Person{} var id = "5b8a45912ed6f24d945bee38" err = c.Find(bson.M{"_id":bson.ObjectIdHex(id)}).Select(bson.M{"username": 1, "score":1, "level": 1}).One(&result) fmt.Println(result) Just It show me :

Need to use pagination in mgo

丶灬走出姿态 提交于 2019-12-24 08:49:32
问题 I am using Go as the back end and MongoDB as the database and use beego framework to develop this application. I would like to do some pagination in my front end. My struct looks like this: type Employee struct { Name string EmpId string Password string PhoneNumber int32 EmailAddress string Position string AccessLevel string Gender string MaritalStatus string Nationality string Department string ICNumber string JoinDate time.Time ConfirmationDate time.Time EndDate time.Time AnnualLeave []

Customize mgo upsert operation

淺唱寂寞╮ 提交于 2019-12-24 05:56:23
问题 I've a game analytics rest API which stores the average performance statistics of the players. When a new statistic arrives, I want to update the existing game record in Mongodb by merging the new delta onto the existing document. I'm storing the past analytics data as well. So that, I can return data like the player's stats are decreasing or increasing since the game's last update. The problem is: When I want to upsert my new game data into Mongodb with mgo, it overwrites all of a player's

mongo aggregation query in golang with mgo driver

China☆狼群 提交于 2019-12-23 12:06:54
问题 I have the following query in mongodb - db.devices.aggregate({ $match: {userId: "v73TuQqZykbxFXsWo", state: true}}, { $project: { userId: 1, categorySlug: 1, weight: { $cond: [ {"$or": [ {$eq: ["$categorySlug", "air_fryer"] }, {$eq: ["$categorySlug", "iron"] } ] }, 0, 1] } } }, {$sort: {weight: 1}}, { $limit : 10 } ); I'm trying to write this in golang using the mgo driver but not able to wrap my head around this at all! How do I translate this to a golang mgo query? 回答1: The examples on the

Golang Bson sort parameters in mgo

烂漫一生 提交于 2019-12-23 10:52:41
问题 I am trying to pass a multiple sort query to the "Sort" parameter of the mgo package (see https://godoc.org/labix.org/v2/mgo#Query.Sort). If the parameters are dynamic (currently held in a slice), how can I translate that into a valid sort string. A working example would be: db.C(Collection).Find(Query).Limit(limit).Sort("-created_when", "-title").Iter() But if "-created_when" and "-title" are held in a slice, and I try using a slice join like: sortBy := []string{"-created_when", "title"} db

How to do text search in mgo?

心不动则不痛 提交于 2019-12-23 09:58:13
问题 I'm trying to search "efg" in field named "abc" c.Find(bson.M{"$text": bson.M{"abc": "efg"}}) c is Collection object. I'm not getting any result. What am I doing wrong? 回答1: You are generating {$text:{abc:"efg"}} , but your query should look like this: {$text:{$search:"efg"}} So try updating your code to: c.EnsureIndexKey("abc") c.Find(bson.M{"$text": bson.M{"$search": "efg"}}) Keep in mind that to search with $text , you need to specify an index. Check out this document that explains how to

MongoDB in Go with mgo, operators with bson.M / bson.D always got syntax error

人盡茶涼 提交于 2019-12-23 07:57:24
问题 It is kind of stupid syntax error, tried tons of ways, just couldn't get it work, someone please help. MongoDB in Go with mgo , I just tried to simplify use the $ne operator, code like below, but kept getting compile syntax error: line 15: convIter := Session.Copy().DB("").C("convs").Find(bson.M { line 16: "conversationStatus": interface{} { line 17: bson.M { line 18: "$ne": "DESTROYED" line 19: }, line 20: }, line 21: }).Iter() Tried to add comma , remove comma everywhere, just couldn't get

i/o timeout with mgo and mongodb

久未见 提交于 2019-12-23 07:27:22
问题 I am running a map-reduce job from mgo. It runs on a collection with a little more than 3.5M records. For some reasons right now I can not port this to aggregation; may be later. So, map-reduce is the thing I am looking forward to. This job, when I run it from the original js files I have created to test the code and output, runs fine. I tried to put the map and reduce code inside two strings and then tried to call the mgo.MapReduce to do the map-reduce for me where I am writing the output in

Golang / MGO — panic: no reachable servers

时光毁灭记忆、已成空白 提交于 2019-12-23 05:47:11
问题 I have the following function that connects to Mongo. For testing, I shutdown mongod, and want to allow the program to continue w/0 mongo if it's not available. It seems that MGO throws a panic if the server can't be connected, so I wrote a defer/recover below, but the panic still causes the program to exit. What's the proper way to recover from this? func connectToMongo(sess *mgo.Session, coll *mgo.Collection, sessionErr error) bool { fmt.Println("enter main - connecting to mongo") // tried

Case insensitive MongoDB query from Go

六眼飞鱼酱① 提交于 2019-12-23 02:21:41
问题 I have this json file: [{ "name": "chetan", "age": 23, "hobby": ["cricket", "football"] }, { "name": "raj", "age": 24, "hobby": ["cricket", "golf"] }] I use this Go code to search the data: id := "ket" regex := bson.M{"$regex": bson.RegEx{Pattern: id}} err = c.Find(bson.M{"hobby": regex}).All(&result) It finds if searched by the same string like "cricket" but if I search string like this "Cricket", it does not find it. 回答1: Add Options: "i" to your RegEx. bson.M{"$regex": bson.RegEx{Pattern: