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
3 answers

How to retrieve a nested array of objects in mongodb with golang?

I am using Golang/Fiber + Mongo driver. I have simple struct for blog post: type Post struct { ID primitive.ObjectID `json:"_id" bson:"_id,omitempty"` Title *string `json:"title" bson:"title"` Slug …
Andrew
  • 701
  • 1
  • 8
  • 19
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

Is there a limit to set minPoolSize for Mongodb?

I'm trying to configure a pool connections to my Go program with mongodb, where I set minPoolSize to 20 connections using mongodb go driver. Something like that: cli, err := mongo.Connect(ctx,…
Lawrence
  • 172
  • 1
  • 9
2
votes
1 answer

How to create/drop mongoDB database and collections from a golang (go language) program.?

func DatabaseConnect() (db *mongo.Database, err error) { ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) defer cancel() client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017")) …
As13
  • 59
  • 6
2
votes
1 answer

Connection is not open when use mongo.Connect instead it does when I do the query

I'm writing a Go app using mongo-driver to connect to the mongo replica set. I've noticed that mongo.Connect is not actually connect to the database. Even if I've shutdown the mongod instance, mongo.Connect sill able to pass through. However, when I…
kitta
  • 1,723
  • 3
  • 23
  • 33
2
votes
1 answer

Find a document via mongo-driver golang with nested array

I'm trying to do a basic query that searches for a document where a specific value is inside an array. Lets take the following example: { "metadata": { "tenant": [ "tenant1", "tenant2", "tenant3" ] } } filter :=…
Gal Sosin
  • 714
  • 7
  • 27
2
votes
1 answer

Mongo Aggregate - $addFields with multiplication

I'm using the official mongo driver on golang and trying to aggregate. I want to sort entries based on the multiplication of currency and salary. import ( "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" …
codemonkey
  • 809
  • 1
  • 9
  • 21
2
votes
3 answers

UpdateOne fails on client due to timeout, but MongoDB processes it anyway

One of my tests for a function that performs increments using the MongoDB driver for Go is randomly breaking in an unexpected way. Here's what the test does: Create a proxy (with toxiproxy) to a local MongoDB instance. Disable the proxy, so the…
Michael Benford
  • 14,044
  • 3
  • 60
  • 60
2
votes
1 answer

How to wait while replicas are caught up master

There is a mongodb cluster (1 master 2 replicas) Updating records in a larger number and for this used BulkWrite, need to call next BulkWrite after replicas caught up master, need to make sure that the replicas have already caught up with the master…
Kitsor
  • 144
  • 2
  • 10
2
votes
1 answer

Golang aggregation group by multiple values with MongoDB

I'm trying to aggregate with the group operator and multiples values I made this query in MongoDB : db.Modules.aggregate([{$group: {_id: {"module":"$module","host":"$host"},"status":{$last:"$status"}}}]) I use package : go.mongodb.org I want to…
All
  • 43
  • 4
2
votes
1 answer

Setting a field as index only if it is not empty

I have a a structure with two keys as follows: { "cannot_be_empty": "hello", "can_be_empty": "world" } Right now I set the index to be a combination of can_be_empty and cannot_be_empty. db.collection.Indexes.CreateOne(IndexModel{{Keys:…
absolutelydevastated
  • 1,657
  • 1
  • 11
  • 28
2
votes
1 answer

How to apply multiple filters on mongodb collection based on user criteria using golang

From my frontend, users can apply filters based on date range or purpose, or both. How do i generate the MongoDB filter so that if users pass in date range, it only uses that as a filter? If it is only purpose, it only uses purpose and if it is…
Mcbaloo
  • 157
  • 5
  • 18
2
votes
1 answer

Get last inserted element from mongodb in GoLang using FindOne and $natural

I'm trying to retrieve the last inserted document using FindOne as suggested elsewhere: collection.FindOne(ctx, bson.M{"$natural": -1}) Get last inserted element from mongodb in GoLang Here is my example: var lastrecord bson.M if err =…
Ironkaiju
  • 21
  • 2
2
votes
1 answer

How to get the value inside a primitive M

showInfoCursor, err := collection.Aggregate(context.TODO(), mongo.Pipeline{unwindStage, groupStage}) if err != nil { panic(err) } var showsWithInfo []bson.M if err = showInfoCursor.All(context.TODO(), &showsWithInfo); err !=…
G. Raven
  • 77
  • 8
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