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…
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": …
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…
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…
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…
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)…
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…
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…
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…
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…
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 …
I have documents similar to this document in my mongo db
{
"_id": ObjectId("5cbf416ec6490b9baff4d284"),
"rewards" : [
{
"percent" : NumberLong(100),
"promotion_id" : "promotest"
}
],
…
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…
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,…
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"
…