mgo

Find by id with mgo

孤街醉人 提交于 2019-11-27 05:55:13
问题 I would like to find a data by _id . I know that this data exists and that this _id exist (I've tested it with pymongo). But the code below doesn't find it: type id_cookie struct { IdCookie int } func get_id_mongo() int { session, err := mgo.Dial("127.0.0.1") if err != nil { panic(err) } defer session.Close() // Optional. Switch the session to a monotonic behavior. session.SetMode(mgo.Monotonic, true) c := session.DB("id_bag").C("id_cookie") data := id_cookie{} err2 := c.FindId(bson.M{"_id":

too many open files in mgo go server

大兔子大兔子 提交于 2019-11-27 04:44:09
问题 I'm getting these errors in the logs: Accept error: accept tcp [::]:80: accept4: too many open files; for a mongodb server on ubuntu, written in go using mgo. They start appearing after it's been running for about a day. code: package main import ( "encoding/json" "io" "net/http" "gopkg.in/mgo.v2/bson" ) var ( Database *mgo.Database ) func hello(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "hello") } func setTile(w http.ResponseWriter, r *http.Request) { var requestJSON map

Unstructured MongoDB collections with mgo

独自空忆成欢 提交于 2019-11-27 04:19:12
问题 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

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

微笑、不失礼 提交于 2019-11-27 02:45:53
问题 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" :

mgo - query performance seems consistently slow (500-650ms)

寵の児 提交于 2019-11-27 02:11:20
My data layer uses Mongo aggregation a decent amount, and on average, queries are taking 500-650ms to return. I am using mgo . A sample query function is shown below which represents what most of my queries look like. func (r userRepo) GetUserByID(id string) (User, error) { info, err := db.Info() if err != nil { log.Fatal(err) } session, err := mgo.Dial(info.ConnectionString()) if err != nil { log.Fatal(err) } defer session.Close() var user User c := session.DB(info.Db()).C("users") o1 := bson.M{"$match": bson.M{"_id": id}} o2 := bson.M{"$project": bson.M{ "first": "$first", "last": "$last",

Efficient paging in MongoDB using mgo

余生颓废 提交于 2019-11-26 20:28:36
问题 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

Best practice to maintain a mgo session

谁说胖子不能爱 提交于 2019-11-26 19:51:13
I'm currently using a mongodb with mgo lib for a web application, but I'm not sure if the way I'm using it, is good one .. package db import ( "gopkg.in/mgo.v2" ) const ( MongoServerAddr = "192.168.0.104" RedisServerAddr = "192.168.0.104" ) var ( MongoSession, err = mgo.Dial(MongoServerAddr) MDB = MongoSession.DB("message") MCol = MDB.C("new") MSav = MDB.C("save") UDB = MongoSession.DB("account") UCol = UDB.C("user") ) I init the db session and create variables who takes the collection and document value, so when I need to query a collection, I use the variable to make it. Like that : func

mgo - query performance seems consistently slow (500-650ms)

偶尔善良 提交于 2019-11-26 08:36:23
问题 My data layer uses Mongo aggregation a decent amount, and on average, queries are taking 500-650ms to return. I am using mgo . A sample query function is shown below which represents what most of my queries look like. func (r userRepo) GetUserByID(id string) (User, error) { info, err := db.Info() if err != nil { log.Fatal(err) } session, err := mgo.Dial(info.ConnectionString()) if err != nil { log.Fatal(err) } defer session.Close() var user User c := session.DB(info.Db()).C(\"users\") o1 :=

Best practice to maintain a mgo session

隐身守侯 提交于 2019-11-26 07:27:16
问题 I\'m currently using a mongodb with mgo lib for a web application, but I\'m not sure if the way I\'m using it, is good one .. package db import ( \"gopkg.in/mgo.v2\" ) const ( MongoServerAddr = \"192.168.0.104\" RedisServerAddr = \"192.168.0.104\" ) var ( MongoSession, err = mgo.Dial(MongoServerAddr) MDB = MongoSession.DB(\"message\") MCol = MDB.C(\"new\") MSav = MDB.C(\"save\") UDB = MongoSession.DB(\"account\") UCol = UDB.C(\"user\") ) I init the db session and create variables who takes