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"`
…
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…
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":{
…
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…
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":…
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 …
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()…
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…
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…
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…
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…
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
…
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…
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…