mgo

How to construct an $or query in mgo

家住魔仙堡 提交于 2019-12-03 10:38:07
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. 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/bson" ) type Person struct { Num int Uuid string Name string } func main() { // Connect to the database session

Empty or not required struct fields in golang

五迷三道 提交于 2019-12-03 04:52:13
问题 I'm somewhat new to typed languages like Go and am trying to learn the best ways to implement things. I have two structs that represent models that will be inserted into a mongodb database. One struct (Investment) has the other struct (Group) as one of its fields. type Group struct { Base Name string `json:"name" bson"name"` } type Investment struct { Base Symbol string `json:"symbol" bson:"symbol" binding:"required"` Group Group `json:"group" bson:"group"` Fields bson.M `json:"fields" bson:

Best practice to maintain a mgo session

匿名 (未验证) 提交于 2019-12-03 02:52:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: 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

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

匿名 (未验证) 提交于 2019-12-03 00:50:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: 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 it work, always got such compile syntax error like

Saving pdf in MongoDb using GoLang mgo

孤街醉人 提交于 2019-12-02 08:03:35
I read multiple blogs about saving the file using mgo, but unable to find a solution for below specific need, help shoutout! Below inserts object in MongoDb: var dbSchoolPojo dbSchool i := bson.NewObjectId() dbSchoolPojo.ID = i coll := db.C("school") coll.Insert(&dbSchoolPojo) Below able to get hold of file: file, handler, err := r.FormFile("pdf") //Able to get file from r *http.Request Now, before inserting object, I need to set above file like: dbSchoolPojo.pdf = file.Bytes(); //Of course Bytes() is invalid method My structs object: type dbSchool struct { ID bson.ObjectId `json:"id" bson:"

Retrieve item list by checking multiple attribute values in MongoDB in golang

好久不见. 提交于 2019-12-01 18:27:28
问题 This question based on MongoDB,How to retrieve selected items retrieve by selecting multiple condition.It is like IN condition in Mysql SELECT * FROM venuelist WHERE venueid IN (venueid1, venueid2) I have attached json data structure that I have used. [Ref: JSON STRUCTUE OF MONGODB ] . As an example, it has a venueList then inside the venue list, It has several attribute venue id and sum of user agents name and total count as value.user agents mean user Os,browser and device information. In

Retrieve item list by checking multiple attribute values in MongoDB in golang

坚强是说给别人听的谎言 提交于 2019-12-01 17:51:46
This question based on MongoDB,How to retrieve selected items retrieve by selecting multiple condition.It is like IN condition in Mysql SELECT * FROM venuelist WHERE venueid IN (venueid1, venueid2) I have attached json data structure that I have used. [Ref: JSON STRUCTUE OF MONGODB ] . As an example, it has a venueList then inside the venue list, It has several attribute venue id and sum of user agents name and total count as value.user agents mean user Os,browser and device information. In this case I used os distribution.In that case i was count linux,ubuntu count on particular venueid. it

Why can't I find the ID using the mgo library of golang?

别等时光非礼了梦想. 提交于 2019-12-01 15:51:45
问题 I am using mgo library for mongo operationg in golang and here is my code : session.SetMode(mgo.Monotonic, true) coll := session.DB("aaaw_web").C("cron_emails") var result Result fmt.Printf("%v", message.ID) err = coll.FindId(bson.ObjectId(message.ID)).One(&result) fmt.Printf("%v", result) fmt.Println(err) I am getting this output : 595f2c1a6edcba0619073263 {ObjectIdHex("") 0 0 0 0 { 0 false 0 } 0 0 0 0 0 0 0} ObjectIDs must be exactly 12 bytes long (got 24) not found But I checked, document

Set default date when inserting document with time.Time field

筅森魡賤 提交于 2019-12-01 10:43:27
In mongoose ( node.js ) I can define a model schema with a default Date.now like so: ... type: Date, default: Date.now ... How do I achieve the same without having to insert the time.Time every time I create a document with mgo ? type User struct { CreatedAt time.Time `json:"created_at" bson:"created_at"` // Make this field filled automatically with time.Now() every time a document of this `struct` is inserted } icza In Go you can't define default values for fields, they will always be the zero-value of their type when a new struct value is created (unless you use a composite literal where you

Mongodb - aggregation $push if conditional

岁酱吖の 提交于 2019-12-01 02:54:20
I am trying to aggregate a batch of documents. There are two fields in the documents I would like to $push. However, lets say they are "_id" and "A" fields, I only want $push "_id" and "A" if "A" is $gt 0. I tried two approaches. First one. db.collection.aggregate([{ "$group":{ "field": { "$push": { "$cond":[ {"$gt":["$A", 0]}, {"id": "$_id", "A":"$A"}, null ] } }, "secondField":{"$push":"$B"} }]) But this will push a null value to "field" and I don't want it. Second one. db.collection.aggregate([{ "$group": "field": { "$cond":[ {"$gt",["$A", 0]}, {"$push": {"id":"$_id", "A":"$A"}}, null ] },