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

Making a unique field in Mongo-go-driver

I am very new to both Go and Mongodb and was writing my first rest-api with Go and Mongo. I am using mongo-go-driver and have the following Modal struct in Go type Modal struct { Group []string `bson:"group" json:"group"` …
Rohit
  • 3,659
  • 3
  • 35
  • 57
3
votes
0 answers

Golang mongo-go-driver hangs/timeout on collection.InsertOne during load test

I'm trying to loadtest a simple go/mongo API with vegeta, but I'm only getting timeout during the test of the POST endpoint. Mongo driver https://github.com/mongodb/mongo-go-driver Handler // CreateAccount handles creation of a new Account func…
Heron Rossi
  • 681
  • 6
  • 18
3
votes
2 answers

mongo-go-driver aggregate call always returns error

I'm trying to do an aggregation with mongo-go-driver (MongoDB team driver for Golang) and I can't see what I'm doing wrong here: // group group, e := bson.ParseExtJSONObject(` { "$group": { "_id":{ …
2
votes
1 answer

incorrectly drafted MongoDB aggregation pipeline $match stage

I'm trying to make a query in golang language (below I've attached working code of pure mongodb query) using go.mongodb.org/mongo-driver/mongo library, below is golang query code. I can't get matchStage to work correctly, I've tried many variants…
alex
  • 524
  • 2
  • 11
2
votes
1 answer

How to create a text index for nested fields that have the same name

I am trying to create a compound text index on 2 nested fields that have the same name. The reason why I am trying to do this is so that I can perform full text search using mongo on both fields. data structure example { "createdAt":…
Nayef Radwi
  • 1,305
  • 13
  • 25
2
votes
1 answer

Sort mongo results if a value exists in the field or not

I have a Student struct which looks like this. type Student struct { Name string `json:"name" bson:"name"` Marks int `json:"marks" bson:"marks"` Subjects …
Rajat Singh
  • 653
  • 6
  • 15
  • 29
2
votes
1 answer

Reusing context.WithTimeout in deferred function

The below code snippet (reduced for brevity) from MongoDB's Go quickstart blog post creates context.WithTimeout at the time of connecting with the database and reuses the same for the deferred Disconnect function, which I think is buggy. func main()…
Aadithya V
  • 47
  • 8
2
votes
2 answers

How to round to at most 2 decimal places, if necessary using golang projection mongodb

projectStage := bson.D{ {"$project", bson.D{ {"_id", 0}, {"name", "$_id"}, {"total", 1}, {"totalPagu", 1}, {"idpagu", 1}, {"pdn", bson.D{ {"$round", bson.D{ {"pdn", 1}, }}, }, }}, }, } I…
user1191933
  • 75
  • 1
  • 10
2
votes
1 answer

Check if value in an object array exist golang

I am trying to check if a value exist on the mongo db before appending new one to it but I keep getting an error every time. obId, _ := primitive.ObjectIDFromHex(id) query := bson.D{{Key: "_id", Value: obId}} var result…
King
  • 1,885
  • 3
  • 27
  • 84
2
votes
1 answer

How to access MongoDB Change Stream data using Go

I am watching a mongodb collection with for all insert events using the golang. I used the mongodb function, changes streams. My requirements is to access the data inside that return event where it seems to be of type bson.m Here's my code…
Jananath Banuka
  • 2,951
  • 8
  • 57
  • 105
2
votes
1 answer

Golang: how to check if collection.Find didn't find any documents?

I'm using Go Mongo documentation where it is explicitly written that with FindOne function, if no documents are matching the filter, ErrNoDocuments will be returned, however, this error is not returned if I use Find function and no documents are…
Mari
  • 155
  • 1
  • 9
2
votes
1 answer

How to Set Limit for options.FindOne() in go mongo-driver

I see there is a way to SetLimit() for Find() func, But I don't see any options to set limit for FindOne() , Since we are searching single result out of FindOne() we don't even have to limit it ? Automatically it handles limit ? Tried setting limit…
Gopher_k
  • 273
  • 2
  • 9
2
votes
1 answer

Is there a way that can find one document and clone it with changing id/value in mongodb with Go

suppose I have code like this: var result bson.M err := coll.FindOne(context.TODO(), filter).Decode(&result) if err != nil { panic(err) } // somehow I can change the _id before I do insertOne …
Lex
  • 25
  • 1
  • 5
2
votes
1 answer

Variable user selection

I need to create some variation for the user, so that he can select only those users for whom he specifies a category (search by category) or those who do not have the same categories as in the array (in the code you can see the array). I used the…
alex
  • 524
  • 2
  • 11
2
votes
1 answer

How can i use $set and $inc in a single update call in mongodb golang?

I tried doing this but it didn't work. update := bson.D{{ "$set": bson.D{ "id": "Q" + addr_to_string }, {"$inc": bson.D{ "amount": amount_int } }}} Error: mixture of field:value and value elements in struct literal This is my initial code: update…
l33t
  • 133
  • 6