Questions tagged [mongo-go]

Official MongoDB Go driver

Official driver page: https://docs.mongodb.com/ecosystem/drivers/go/

It is available here: https://github.com/mongodb/mongo-go-driver

Documentation (godoc): https://godoc.org/github.com/mongodb/mongo-go-driver

It was announced here: Considering the Community Effects of Introducing an Official MongoDB Go Driver

229 questions
2
votes
1 answer

Marshal/Unmarshal custom type with mongodb-go-driver

So I'm trying out the official mongodb go driver (was using mgo before), and I'm stuck. I'm trying to marshal/unmarshal a custom type, which is obviously not working by default. The documentation is kind of lacking. Basically what I would like to do…
user7632204
  • 21
  • 1
  • 3
2
votes
3 answers

How to use the elements of a slice in bson.A using mongo-go-driver 0.2.0

How do I use a given slice to generate a bson.A? I need this to make dynamic queries. I am upgrading from mongo-go-driver alpha to beta 0.2.0, and this has become a major change in my project since the API is different…
faraonc
  • 61
  • 1
  • 6
2
votes
1 answer

How to run mongo command with mongo-go-driver?

Hi there :) I'm working on a golang app linked to mongo DB (I use the official driver: mongo-go) and here's my problem,I want to execute this function db.rmTickets.find().forEach(function(doc) { doc.created=new Date(doc.created) …
G.D
  • 802
  • 2
  • 7
  • 15
2
votes
2 answers

How to Find and compare Dates on Official MongoDB Go Driver?

I am new to mongodb-go-driver and i am stuck. I have a date inside a struct like: type Email struct { Date string `json:"date"` } the Dates on my mongoDB and mapped in my struct have the values like "02/10/2018 11:55:20". I…
Igor Silva
  • 41
  • 1
  • 5
2
votes
1 answer

How to build queries with comparison operators using mongodb official driver?

I need to build a query using comparison operators, equivalent of db.inventory.find( { qty: { $gt: 20 } using the official driver. Any idea how to do that?
mike
  • 533
  • 1
  • 7
  • 24
1
vote
1 answer

Adding new values to an array from another array of same type MongoDB-GO

I'm currently working with the mongodb driver on golang and trying to add fields to a document's array from an input array of the same type: type Organization struct { ID string `bson:"_id,omitempty" json:"id,omitempty" ` Name string…
yieniggu
  • 384
  • 1
  • 7
  • 20
1
vote
0 answers

Mongodb Connection leak using mongo-go driver

I'm developing a Go application that integrates with MongoDB to create, read, and delete images. I've set the connection pool to 4 for testing purposes, but I've noticed that the number of connections can go up to 10 even if I haven't hit any…
1
vote
1 answer

Why is a simple query taking more than 2 seconds with Golang Mongo driver?

I am coding a golang web service that has a mongo database, I am using the go.mongodb.org/mongo-driver v1.11.6 and a simple query is taking more than 2 seconds to complete. The database has only a few records, is it just for testing, no more than 10…
1
vote
1 answer

Go MongoDB driver connects successfully to database but won't retrieve anything

I'm trying to connect to my mongo Atlas database using golang mongodb official driver. The problem is that the connection seems successful but the client won't retrieve anything. I tried to log ListDatabaseNames() and ListCollectionNames() but it…
1
vote
1 answer

How mock mongo.ErrNoDocuments with mtest in Golang

I have this function: func CheckExistUser(documentNumber string, mongoClient *mongo.Database) error { ctx := context.Background() colletionUsersName := "my-collection" colletionUsers := mongoClient.Collection(colletionUsersName …
Renato Cassino
  • 792
  • 1
  • 7
  • 27
1
vote
1 answer

MongoDB Golang: FindOne() with nested Bson.M failed for 10% attempts, Bson.M+Bson.D or a single Bson.M always work, why?

Need some help to figure out why nested bson.M doesn’t work occasionally in FindOne(). For the following Golang structs stored in a MongoDb collection for type A: type A struct { Id primitive.ObjectID Random1 string Parents []B Random2…
Tianjun Fu
  • 11
  • 2
1
vote
1 answer

Golang Struct for Nested Objects

I am currently creating a golang api using GoFiber V2. I have the following document structure for a music track in a Mongo database: { "_id" : ObjectId("63cc26cb86ae1611380e1206"), "active" : 1, "exclusive" : "false", "track_title"…
Jamie_D
  • 979
  • 6
  • 13
1
vote
1 answer

Redefine the bson tag in golang, how to be compatible with the fields in the database with lowercase field names?

There are many structures(There are roughly hundreds of structures, all generated automatically by a tool.) like this: type ChatInfo struct { Name string `json:"name"` IconFrame uint64 `json:"iconFrame"` } The bson tag was…
small white
  • 389
  • 2
  • 11
1
vote
1 answer

How would I update multiple records based on different key in Mongo in one query?

If I have something similar to the following... collection.InsertMany(context.TODO(), []interface{}{ bson.M{ "_id" : 1, "member" : "abc123", "status" : "P" }, bson.M{ "_id" : 2, "member" : "xyz123", "status" : "A" }, bson.M{ "_id" : 3,…
haroldcampbell
  • 1,512
  • 1
  • 14
  • 22
1
vote
1 answer

What is the fastest way to decode a nested MongoDB document array into a struct slice in Go?

Here's my problem. I'm using gqlgen library to run a GraphQL server. In my database I have a MongoDB document that looks like this: { "_id": ObjectID(...), "somefield": "data", "anArrayOfObjects": [ { "field1": "value1", …
axelrogg
  • 15
  • 5