Questions tagged [mongo-go-driver]
44 questions
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
3
votes
1 answer
How to update documents using the value from inner array
I am getting stuck on something that doesn't seem that complicated, maybe there is something I didn't think about or saw.
Having (a lot) of documents containing an array of objects, like those:
{
"_id": "Ox1",
"results": [
{
…

Ezechiel
- 69
- 6
3
votes
1 answer
Mongodb storage and retreival in mongogo driver
I am trying to insert the data and read that data from mongodb using mongo go driver. I am using a struct which has a data field. When I am using the data type as interface I get multiple maps and when I specify it as slice of maps it returns a…

R0512S
- 53
- 4
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
2
votes
2 answers
How to use “Let” Aggregate options in mongo-go-driver?
Trying to use mongo-go-driver SetLet in the aggregate options to pass user variables defined earlier in my function into my aggregation pipeline.
Trying to parse the documentation for this…
SetLet asks for just an interface{} for the input…

Tyler
- 91
- 4
2
votes
1 answer
How to db.getUser() using go mongo-driver
I want to get the user details for a db using go driver.
For ex. in mongoshell
> db.getUser("testuser")
null
How do i construct a bson.M or bson.D for this ?
I don't want to pass additional args just retrieve the userinfo of a db
var op…

Gopher_k
- 273
- 2
- 9
2
votes
0 answers
Mongo db driver doesn't update record
I'm creating an app using microservice architecture with go and mongodb. Now I'm trying to make a crud with users, but I faced with a small problem. When I try to update the record nothing changes. I don't know what can be the problem, cuz I took it…

Cross
- 497
- 1
- 3
- 13
2
votes
0 answers
MongoDB: How to query objects using id references?
So basically I am trying to query a collection with respect to another collection. More specifically, I have an items collection and a purchases collection. The purchases collection's documents hold references to an item document and a user document…

camelCaseIsGoodPractice
- 168
- 1
- 11
2
votes
0 answers
Golang-Mongodb Insert element of array inside another array maintaining uniqueness of array
Currently I am learning learn to create restful api with golang and mongodb. I am using mongo-go-driver. I got stuck at this.
Let's say I have a collection that has document entries with the structure like below.
{
"_id" :…

Vishal Kumar Mahto
- 21
- 2
2
votes
1 answer
how to resume Mongodb M0 Cluster?
MongoDB cluster(free) paused only after a couple hours of inactivity (as opposed to the stated 7 days for the free cluster).
I have tried to resume using mongosh and through my Go code but it still does not work. This is my second time encountering…

דברי
- 41
- 5
2
votes
1 answer
how to use golang mongodb driver query $or with $and?
1.judge field label_status is exists, if exists is true find one record.
2.if field label_status exist is false. find one record label_status eq 1 and author
eq sample
i use the code:
bson.D{
{"$or", bson.D{{"label_status",…

Yu.jian
- 21
- 1
2
votes
3 answers
unable to authenticate with mongodb golang driver
I'm using mongodb community version 4.2.13 and go driver version 1.5.
My go application is running on the same host as db, but getting the following error when trying to make a connection:
connection() error occured during connection handshake: auth…

call-me-corgi
- 193
- 4
- 10
2
votes
2 answers
How to replace a document in MongoDB with Go driver?
I'm trying to update a document in MongoDB with mongodb/mongo-go-driver. From its doc, one document can be replaced with:
var coll *mongo.Collection
var id primitive.ObjectID
// find the document for which the _id field matches id and add a field…

Swix
- 1,883
- 7
- 33
- 50
2
votes
1 answer
remove a field from mongodb query result in golang
This is my function from mongodb-go-driver:
func MongodbFindOne(key, value string) bson.M {
var result bson.M
opts := options.FindOne().SetShowRecordID(false)
_ = Collection.FindOne(context.TODO(), bson.M{key: value},…

CheckYourSec
- 143
- 3
- 11
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…

ODawah
- 11
- 2