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

How to BulkWrite\UpdateMany with Go driver for MongoDB

I'm migrating from mgo driver, and my function looks like this: queue := collection.Bulk() for j := range changes { .. queue.Update(doc, update) } saveResult, err := queue.Run() This makes some $push and $set updates to a single document in…
Dennis S
  • 315
  • 3
  • 13
6
votes
2 answers

Using time.Time in mongodb record

I'm inserting new item in collection. Using official mongo go driver for this (https://github.com/mongodb/mongo-go-driver). collection.InsertOne(context.Background(), map[string]interface{}{ "string": "test", "integer": 123, "float": …
maksim
  • 458
  • 2
  • 6
  • 16
5
votes
1 answer

Autofill created_at and updated_at in golang struct while pushing into mongodb

type User struct { ID primitive.ObjectID `bson:"_id,omitempty"` CreatedAt time.Time `bson:"created_at"` UpdatedAt time.Time `bson:"updated_at"` Name string `bson:"name"` } user…
Neeraj Sharma
  • 73
  • 1
  • 6
5
votes
2 answers

With mongo-go-driver, how do I efficiently retrieve duplicated field name from WriteError?

I have three unique indexes in my collection. When user accidentally insert a data having a duplicate in the field B, how do I know that the duplication comes from field B? On unique index constraint violation, mongo-go-driver behavior is returning…
imeluntuk
  • 385
  • 1
  • 5
  • 15
5
votes
2 answers

How I convert a BSON Document into a map[string]interface{}

I was trying to decode the data of the cursor into a map[string]interface{}, I tried it directly but it doesn't works at all, so I fount that I have to convert it to a BSON document and next convert it to a map[string]interface{}, and finally into…
Marco
  • 161
  • 1
  • 1
  • 8
4
votes
1 answer

Mongodb Timeseries / Golang - ['timestamp' must be present and contain a valid BSON UTC datetime value]

I have the following example Go code that inserts data into MongoDB coming from a REST request (GIN), but it fails with: ['timestamp' must be present and contain a valid BSON UTC datetime value] code: func CreateDevicesReadings(c *gin.Context)…
newduino
  • 179
  • 1
  • 6
  • 16
4
votes
1 answer

Go Mongo Update NonZero values only

How to update the document with non zero values only. As example I didn't received any value for status and Struct has only two values to be updated. So it should only update those 2 values and skip zero/null values. But as given below it's updating…
Zayn Korai
  • 493
  • 1
  • 6
  • 24
4
votes
2 answers

Mock UpdateResult From UpdateOne Using Go Mongo-Driver And mtest

I'm trying to use the mtest package (https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo/integration/mtest) to perform some testing with mock results on my MongoDB calls, but I can't seem to figure out how to properly mock the *mongo.UpdateResult…
Francis Bartkowiak
  • 1,374
  • 2
  • 11
  • 28
4
votes
2 answers

How to obtain MongoDB version using Golang library?

I am using Go's MongodDB driver (https://pkg.go.dev/go.mongodb.org/mongo-driver@v1.8.0/mongo#section-documentation) and want to obtain the version of the mongoDB server deployed. For instance, if it would been a MySQL database, I can do something…
Bharath Ram
  • 190
  • 1
  • 12
4
votes
1 answer

custom encoder/decoder for insert or geting documents for mongo-driver

I have read this friendly article about encoding and decoding custom object using official go mongo driver. There is nice example how to marshaling them into extended json format (bson.MarshalExtJSONWithRegistry). But I would like to know how to put…
S.R
  • 2,411
  • 1
  • 22
  • 33
4
votes
2 answers

Necessity of bson struct tag when using with MongoDB go client

I am watching a tutorial on how to create Go restful APIs that use MongoDB for persistence (this one to be more precise). The instructor is using in his model (struct) both json and bson tags, something like type NoteUpdate struct { ID …
pkaramol
  • 16,451
  • 43
  • 149
  • 324
4
votes
1 answer

go mongo driver update unable to set array of objects to null

I have documents similar to this document in my mongo db { "_id": ObjectId("5cbf416ec6490b9baff4d284"), "rewards" : [ { "percent" : NumberLong(100), "promotion_id" : "promotest" } ], …
Srinivas
  • 294
  • 3
  • 18
4
votes
1 answer

Not able to use mongodb transactions using mongo go driver

I have created a mongodb replica set. I am able to run transactions from the mongo shell. But when I try to do it using mongo-go-driver I always get this error (IllegalOperation) Transaction numbers are only allowed on a replica set member or…
revant
  • 234
  • 3
  • 13
4
votes
2 answers

Watch for MongoDB Change Streams

We want our Go application to listen to the data changes on a collection. So, googling in search for a solution, we came across MongoDB's Change Streams. That link also exhibits some implementation snippets for a bunch of languages such as Python,…
vahdet
  • 6,357
  • 9
  • 51
  • 106
3
votes
0 answers

MongoDB Go Driver's UnmarshalBSONValue - can I set a pointer struct field to nil if value is a BSON null (without omitempty)?

Consider my custom string-type Id which marshals/unmarshals to/from a BSON ObjectId (we abstract-away the underlying BSON at our data layer; our services are agnostic to our storage): package id import ( "fmt" …
crunk1
  • 2,560
  • 1
  • 26
  • 32
1
2
3
15 16