mgo

Marshal into a bson.Raw

被刻印的时光 ゝ 提交于 2019-12-11 13:06:51
问题 Using gopkg.in/mgo.v2/bson, I wonder how to marshal an interface{} value into a value of type bson.Raw . The documentation for bson.Raw states: Using this type it is possible to unmarshal or marshal values partially. But I can't find a Marshal function that would return bson.Raw . What am I missing? Example of what I try to do: package main import ( "fmt" "gopkg.in/mgo.v2/bson" ) func main() { // How to avoid a MarshalRaw help function? raw, err := MarshalRaw("Hello world") if err != nil {

Why won't mgo unmarshall my struct properly?

此生再无相见时 提交于 2019-12-11 10:37:54
问题 Earlier I posted this question asking about writing custom BSON marshalling/unmarshalling in Go using mgo. Now I've come to test it I think I've hit on a bigger problem. All my structs unmarshal to nil values. This is my currency struct with the implementations of bson.Getter and bson.Setter: type Currency struct { value decimal.Decimal //The actual value of the currency. currencyCode string //The ISO currency code. } /* GetBSON implements bson.Getter. */ func (c Currency) GetBSON()

Mocking mgo chaining functions

跟風遠走 提交于 2019-12-11 09:05:53
问题 While testing some of my codebase I found it useful to mock out objects from "gopkg.in/mgo.v2" with interfaces. I'm running into a problem where mgo.Query does not implement my interface Query. import mgo "gopkg.in/mgo.v2" type Collection interface { FindId(interface{}) Query Find(interface{}) Query UpdateId(interface{}, interface{}) error Update(interface{}, interface{}) error UpsertId(interface{}, interface{}) (interface{}, error) Insert(...interface{}) error RemoveId(interface{}) error }

Randomize order of a MongoDB query in Go

瘦欲@ 提交于 2019-12-11 07:18:42
问题 Here is my query : c := session.DB("searchV").C("video") var results []BadVideo err5 := c.Find(nil).All(&results) fmt.Println("request done") if err5 != nil { panic(err5) } var i = 0 for _,badvideo := range results { } I would like to randomize the order of browsing the items of the query for making operation on each item of the request ... So each time I run it, I browse it in a different order. 回答1: Manual shuffling Here's a simple shuffle algorithm, which shuffles (randomizes) a []BadVido

Issue in mongo query with multiple conditions by using golang

陌路散爱 提交于 2019-12-11 06:55:12
问题 I have a document as follows - { "_id" : "580eef0e4dcc220df897a9cb", "brandId" : 15, "category" : "air_conditioner", "properties" : [ { "propertyName" : "A123", "propertyValue" : "A123 678" }, { "propertyName" : "B123", "propertyValue" : "B123 678" }, { "propertyName" : "C123", "propertyValue" : "C123 678" } ] } In this, the properties array can have multiple number of elements. When I perform a search via my API, I would ideally pass an array similar to properties in the body of my POST

Mgo aggregation: how to reuse model types to query and unmarshal “mixed” results?

若如初见. 提交于 2019-12-11 06:34:28
问题 Let's say we have 2 collections: "users" and "posts" , modeled by the following types: type User struct { ID string `bson:"_id"` Name string `bson:"name"` Registered time.Time `bson:"registered"` } type Post struct { ID string `bson:"_id"` UserID string `bson:"userID"` Content string `bson:"content"` Date time.Time `bson:"date"` } These can be used when storing / retrieving individual or even collection of documents, e.g.: usersColl := sess.DB("").C("users") postsColl := sess.DB("").C("posts"

How to write go bson.M from MongoDb query

只谈情不闲聊 提交于 2019-12-11 06:15:47
问题 I am using mongodb as database. I'm able to query the database from the command line using the command db.nfinstances.distinct("ipv4Addresses",{"nfType":"AMF", "amfInfo.amfSetId": "3fa85f64-5717-4562-b3fc-2c963f66af33"}) and this give me the ip address [x.x.x.x] output that I want. However, if I query using the golang query var SliceIP []NfInstance db.C(COLLECTION).Find(bson.M{ "nfType": "AMF", "amfInfo.amfSetId": "3fa85f64-5717-4562-b3fc-2c963f66af33"} ).Distinct("ipv4Addresses", &SliceIP) I

retrieve a nested document with mgo

限于喜欢 提交于 2019-12-11 06:13:58
问题 I need to retrieve a nested document in mongoDB with mgo. Here is my document in db: { "_id" : "packing_type_0000", "name" : "packing", "category" : "logistics", "en" : { "translatedName" : "Packing and Order Prep", }, } This is my golang structure: type jobTypeWording struct { translatedName string `json:"translatedName" bson:"translatedName"` } type jobType struct { ID string `json:"_id" bson:"_id"` Name string `json:"name" bson:"name"` Category string `json:"category" bson:"category"` en

No reachable servers on static linked go binary

半腔热情 提交于 2019-12-10 18:21:43
问题 Depending on where my binary is being executed, I get different results on mgo Dial. Right now, I'm building on my machine (Fedora: uname -a: Linux localhost.localdomain 4.15.6-300.fc27.x86_64 #1 SMP Mon Feb 26 18:43:03 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux) using the following command: $ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -ldflags '-s' -o myProgram So, if I build my docker image using: FROM centos COPY myProgram "/usr/local/bin/myProgram" ENTRYPOINT ["/usr/local/bin

MongoDB Aggregate lookup in Go (mgo.v2)

家住魔仙堡 提交于 2019-12-10 15:56:09
问题 I'm trying to implement $lookup functionality in one of my mongoDB queries in go (golang) using the mgo package. Below are my collections: folders: "_id" : ObjectId("22222222222222"), "name" : "Media", "level" : 1, "userIDs": [ObjectId("4444444444444")] documents: "_id" : ObjectId("11111111111111"), "title" : "Media Management", "body" : BinData(0,"PvQ6z2NBm4265duo/e2XsYxA5bXKo="), "level" : 1, "folderID" : ObjectId("22222222222222"), // Foreign Key/Field "userIDs" : [ObjectId("44444444444444