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
6
votes
1 answer

Compare consistency models used in mgo

MongoDB servers are queried with multiple consistency rules. In mgo, SetMode of the Session object changes the consistency mode for the session. Three types of consistency modes are available: Eventual, Monotonic, and Strong. e.g. session, err :=…
Tim
  • 1
  • 141
  • 372
  • 590
6
votes
2 answers

How to remove a single document from MongoDB using Go

I am new in golang and MongoDb. How can I delete a single document identified by "name" from a collection in MongoDB? Thanks in Advance
Arjun Ajith
  • 1,850
  • 5
  • 21
  • 46
6
votes
1 answer

Golang Bson sort parameters in mgo

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…
Glenn Walker
  • 264
  • 1
  • 3
  • 8
6
votes
2 answers

Golang mongodb remove all items from collection [mgo.v2]

How to remove all items from collection stored in mongodb using GO lang? In mongo console I can use: db.mycollection.remove({}) where empty brackets {} mean all document pattern. In GO lang (I use "gopkg.in/mgo.v2" and "gopkg.in/mgo.v2/bson")…
user61253764
  • 478
  • 5
  • 9
6
votes
1 answer

Organize Go code for CRUD operations on MongoDB

I'm writing a web application in Go but I have some troubles getting my code organized. For basic CRUD operations on MongoDB I always have to do something like this in the beginning of my code: session, err := mgo.Dial("localhost") if err != nil { …
roeland
  • 6,058
  • 7
  • 50
  • 67
6
votes
2 answers

Golang mongodb mgo driver Upsert / UpsertId documentation

The mongodb documentation says: The fields and values of both the and parameters if the parameter contains only update operator expressions. The update creates a base document from the equality clauses in the parameter, and then applies the…
Gabriel Díaz
  • 161
  • 1
  • 1
  • 5
6
votes
2 answers

mgo, mongodb: find one document that is embedded and part of an array

2 Parts to the question. 1 is the mongodb query itself, the next is how to do it in mgo. How do I query for 1 document of type category (the result should be of type category) where the slug: "general"? The reason I picked this layout is because I…
user2312578
6
votes
1 answer

unit testing golang handler

Here's a handler I wrote to retrieve a document from mongodb. If the document is found, we will load and render the template accordingly. If it fails, it will redirect to 404. func EventNextHandler(w http.ResponseWriter, r *http.Request) { …
Calvin Cheng
  • 35,640
  • 39
  • 116
  • 167
6
votes
1 answer

Using golang and mgo, how do I search for a range of values in MongoDB?

I worked through the example on the mgo homepage, but I'm struggling to find a way to query a range of values. The line: searchResults, searchErr = SearchReading(bson.M{"k": key, "t": { $gte: start, $lte: end } }, limit) fails with: line67: syntax…
Jaco Briers
  • 1,703
  • 1
  • 21
  • 34
5
votes
1 answer

Map Mongo _id in two different struct fields in Golang

I am working on a project that uses combination of Go and MongoDB. I am stuck at a place where I have a struct like: type Booking struct { // booking fields Id int `json:"_id,omitempty"…
Amandeep kaur
  • 985
  • 3
  • 15
  • 35
5
votes
2 answers

Retrieve unstructured array from mongodb in golang

I have the following document in MongoDB { "_id" : ObjectId("57e4f8f454b9a4bb13a031d8"), "ip" : "192.168.0.1", "browser" : "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR…
pidgebob
  • 65
  • 1
  • 4
5
votes
1 answer

GoLang mgo - mgo.ErrNotFound for find(...).All(...)

I have a GoLang code: c.Find(selectQuery).All(&results) if err == mgo.ErrNotFound { // error handling } selectQuery value is not important here. I never get error ErrNotFound. Even if the query doesn't match any results I don't get ErrNotFound. I…
poulius
  • 53
  • 1
  • 6
5
votes
1 answer

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

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…
TheJediCowboy
  • 8,924
  • 28
  • 136
  • 208
5
votes
1 answer

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

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…
Sandun Priyanka
  • 591
  • 7
  • 24
5
votes
2 answers

How to $push in golang for nested array?

I tried to push some data in nested array using $push. Here is my json file { "_id" : ObjectId("57307906f051147d5317984e"), "user" : [ { "firstName" : "chetan", "lastName" : "kumar", "age" : 23, …
Chetan Kumar
  • 328
  • 1
  • 4
  • 18