Questions tagged [mgo]

mgo (pronounced as mango) is a MongoDB driver for the Go language that implements a rich and well tested selection of features under a very simple API following standard Go idioms.

mgo (pronounced as mango) is a driver for the language that implements a rich and well tested selection of features under a very simple API following standard Go idioms.

Note that the "original" mgo driver (github.com/go-mgo/mgo) developed by Gustavo Niemeyer has gone dark (unmaintained). Even the author himself recommends to use the community supported fork github.com/globalsign/mgo which is in much better shape.

Links

644 questions
89
votes
10 answers

server returned error on SASL authentication step: Authentication failed

The following is my MongoDB connection dial from GoLang. But it's returning a panic "server returned error on SASL authentication step: Authentication failed.". My username, password, hostAddrs and dbName are correct. What am I missing here? dbName:…
Arjun Ajith
  • 1,850
  • 5
  • 21
  • 46
59
votes
2 answers

Empty or not required struct fields

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…
msecret
  • 668
  • 1
  • 5
  • 6
47
votes
4 answers

How to install golang 3rd-party projects from download sources?

I'm trying to install mgo which is a mongo-driver written in golang. The standard command: go get launchpad.net/mgo But it failed because of some cert issues. So I manually download the sources of mgo to local E:\mgo, but I don't know to how…
Freewind
  • 193,756
  • 157
  • 432
  • 708
43
votes
4 answers

Mongodb - aggregation $push if conditional

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…
Artorias
  • 1,004
  • 1
  • 12
  • 22
29
votes
3 answers

Best practice to maintain a mgo session

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 =…
JonathanChaput
  • 334
  • 1
  • 4
  • 9
28
votes
3 answers

Why does mgo not return the ID of inserted document?

According to the documentation (http://godoc.org/launchpad.net/mgo/v2) you can obtain the ID of your "Upserted" document if you use the Upsert method. There is also an Insert method that does not provide this functionality. Why is that? What if I…
Sebastián Grignoli
  • 32,444
  • 17
  • 71
  • 86
22
votes
3 answers

Cannot retrieve "_id" value using mgo with golang

This is my struct definition: type Article struct { Id bson.ObjectId `json:"id" bson:"_id,omitempty"` Title string `json:"title"` Author string `json:"author"` Date string `json:"date"` Tags…
dyzdyz010
  • 2,256
  • 3
  • 16
  • 17
21
votes
2 answers

How do you select all records from a mongodb collection in golang using mgo

In MongoDB doing something like db.mycollection.find() returns all documents in a collection. When working in GoLang using the package labix.org/v2/mgo and I do for example: query := db.C("client").Find(); It complains that it requires input in the…
Dean
  • 1,226
  • 2
  • 20
  • 39
20
votes
3 answers

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

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" :…
manigandand
  • 2,094
  • 2
  • 15
  • 21
20
votes
2 answers

Unstructured MongoDB collections with mgo

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…
kwolfe
  • 1,663
  • 3
  • 17
  • 27
18
votes
5 answers

How to marshal json string to bson document for writing to MongoDB?

What I am looking is equivalent of Document.parse() in golang, that allows me create bson from json directly? I do not want to create intermediate Go structs for marshaling
Ganesh
  • 285
  • 1
  • 3
  • 10
17
votes
2 answers

How to construct an $or query in mgo

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":…
if __name__ is None
  • 11,083
  • 17
  • 55
  • 71
17
votes
1 answer

Storing nested structs with mgo

I'm trying to build a mongo document from a go struct that is heavily nested, and I'm running into a problem with the transition from go struct to a mongo object. I've built a very simplified version of what I'm trying to work with here:…
Verran
  • 3,942
  • 2
  • 14
  • 22
17
votes
2 answers

Golang/mgo: How can I ask MongoDB to use current time in a field?

I have this struct that matches the types of a MongoDB collection I'm using: type AppInstance struct { Id bson.ObjectId "_id,omitempty" Url string Priority int LastSeen string } I want the LastSeen field to hold the time of the last…
Sebastián Grignoli
  • 32,444
  • 17
  • 71
  • 86
16
votes
3 answers

Select column from Mongodb in golang using mgo

As I know, we can use > db['twitter-3'].find({}, {"text": 1}) to select all texts in collection. How can we use mgo to find specific field in golang? I tried var result []string err = conn.Find(bson.M{}, bson.M{"text", 1}).All(&result) But it is…
Wyatt
  • 1,357
  • 5
  • 17
  • 26
1
2 3
42 43