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
16
votes
2 answers

MongoDB in Go (golang) with mgo: how to use logical operators to query?

I would like to run the following query in golang using mgo in a pipeline. {"key1" : 1, "$or" : [{"key2" : 2}, {"key3" : 2}]} I have looked everywhere, but I cannot find an example like this. I have tried many different combinations, for…
p.paolo321
  • 193
  • 1
  • 1
  • 7
15
votes
5 answers

Go: how to run tests for multiple packages?

I have multiple packages under a subdirectory under src/, running the tests for each package with go test is working fine. When trying to run all tests with go test ./... the tests are running but it fails.. the tests are running against local…
Gal Ben-Haim
  • 17,433
  • 22
  • 78
  • 131
14
votes
4 answers

What would be the best approach to converting protoc generated structs from bson structs?

I'm writing a RESTful API in Golang, which also has a gRPC api. The API connects to a MongoDB database, and uses structs to map out entities. I also have a .proto definition which matches like for like the struct I'm using for MongoDB. I just…
Ewan Valentine
  • 3,741
  • 7
  • 43
  • 68
14
votes
1 answer

Connecting to MongoDB Atlas using Golang mgo: Persistent no reachable server to replica set

I have a replica set from MongoDB atlas, to which I can connect with ANY other language, and regular mongo client, with the URL provided with the format…
hece
  • 364
  • 2
  • 15
12
votes
1 answer

Find by id with mgo

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 :=…
Bussiere
  • 500
  • 13
  • 60
  • 119
12
votes
1 answer

Mgo omit field even when not empty

I was wondering if there is any way to have a stuct field that doesn't get commited to mgo even if it isn't empty. The only way I have found to do this is to make the field lowercase, which makes it a pain to access. Is there another way? This is an…
Gary
  • 443
  • 9
  • 22
11
votes
1 answer

How to check if collection exists or not MongoDB Golang

I am new to GO language and I am using MongoDB with it. I am creating a backend for an application and its frontend on Angular 4. I want to check if collection exists or not. Here is my code and I have checked it using nil. collection :=…
Habib
  • 846
  • 2
  • 9
  • 24
11
votes
3 answers

Inserting data into MongoDB with mgo

I'm trying to insert some data in MongoDB using Go. Here is the data struct: type Entry struct { Id string `json:"id",bson:"_id,omitempty"` ResourceId int `json:"resource_id,bson:"resource_id"` Word string…
if __name__ is None
  • 11,083
  • 17
  • 55
  • 71
10
votes
1 answer

Efficient paging in MongoDB using mgo

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…
icza
  • 389,944
  • 63
  • 907
  • 827
10
votes
1 answer

Delete all the document older than a date using _id in mongo using mgo

I'm working in Golang and mgo and I would like to delete all the documents in a collection older than a specified date, using _id value. So far I've tried to create a dummy objectId using a struct NewObjectIdWithTime after that I'm trying to delete…
Bestbug
  • 475
  • 4
  • 13
10
votes
1 answer

How to dereference dbref in mgo?

var ( type User struct{ Id bson.ObjectId `bson:"_id"` Name string } type Post struct{ Id bson.ObjectId `bson:"_id"` Uid string User User ref mgo.DBRef Title string } ) //try 10000 times inserts id…
benyu
  • 121
  • 8
10
votes
1 answer

i/o timeout with mgo and mongodb

I am running a map-reduce job from mgo. It runs on a collection with a little more than 3.5M records. For some reasons right now I can not port this to aggregation; may be later. So, map-reduce is the thing I am looking forward to. This job, when I…
SRC
  • 2,123
  • 3
  • 31
  • 44
10
votes
2 answers

partial update using mgo

I have the following problem. I need to convert a structure to map[string]interface{} in order to perform an update in database (using mgo as driver for mongodb). Update For partially updating a document in mongoDB, the (optimal) solution is to…
eAbi
  • 3,220
  • 4
  • 25
  • 39
10
votes
1 answer

How to represent an array with mixed types

I am constructing an aggregation pipeline query with the $substr command from MongoDB but I don't know how to represent the array it requires in Go with the mgo driver because it contains different types of values (string, int). Here is the query in…
Makis Tsantekidis
  • 2,698
  • 23
  • 38
10
votes
3 answers

MongoDB in Go (golang) with mgo: How do I update a record, find out if update was successful and get the data in a single atomic operation?

I am using mgo driver for MongoDB under Go. My application asks for a task (with just a record select in Mongo from a collection called "jobs") and then registers itself as an assignee to complete that task (an update to that same "job" record,…
Sebastián Grignoli
  • 32,444
  • 17
  • 71
  • 86
1
2
3
42 43