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

Mongodb doesn't retrieve all documents in a collection with 2 million records using cursor

I have a collections of 2,000,000 records > db.events.count(); │ 2000000 and I use golang mongodb client to connect to the database package main import ( "go.mongodb.org/mongo-driver/bson" …
Anh Thi
  • 51
  • 1
  • 5
2
votes
2 answers

How to execute FindOne() with logical query selector($or) in Official Go MongoDB driver?

I am a beginner in mongo with go. I am trying to find one document where "username" or email will be matched. But can't figure out the way to implement this condition to filter. Here is my document model: type User struct { Username string …
Abid
  • 477
  • 1
  • 4
  • 15
2
votes
2 answers

What's the way to implement struct field validations in official Go mongo-driver?

I am using Go official MongoDB driver. Is there any way to validate struct fields using BSON tags, the way how GORM pkg does?
Abid
  • 477
  • 1
  • 4
  • 15
2
votes
2 answers

SORT is not working in Aggregate function

I have this code to Mongo in Golang cond := make([]bson.M, 0) cond = append(condiciones, bson.M{"$match": bson.M{"userId": ID}}) cond = append(condiciones, bson.M{ "$lookup": bson.M{ "from": "invoices", …
Elba Nanero
  • 507
  • 1
  • 4
  • 10
2
votes
1 answer

Mongo Go Driver - Inline with Embedded Structs Not working

While working on Golang Mongo Driver, I am stuck with this weird beahviour to use inline Seems like bson:",inline" doesnt work with Embedded Structs. Not able to understand why such a behaviour? inline Inline the field, which must be a struct or…
Art
  • 414
  • 7
  • 24
2
votes
2 answers

MongoDB Auto Increment ID with Golang mongo-driver

Based on the documentation, go.mongodb.org/mongo-driver doesn't seem to provide a way to auto-increment ID when it is upserting a document that has not provided an ID. type Document struct { ID int `bson:"_id"` Foo…
gogofan
  • 533
  • 1
  • 10
  • 20
2
votes
1 answer

Managing schema changes with MongoDB

How to handle if document structure after production changes. Suppose I had 500 documents like this: { name: ‘n1’ height: ‘h1’ } Later if I decide to add all the documents in below format: { name: ‘n501’ height: ‘h501’ weight: ‘w501’ } I am using…
2
votes
1 answer

Is there a way to convert a JSON string into a Mongo extended JSON using Go?

I have a sample JSON body that contains some strings. I want some of the strings to be converted into Mongo Extended JSON. For example, the JSON body passed in is the following: { "GuidBinary": "734cba69-4851-4869-8d0e-e870d6fb3065", …
Riaru Neimu
  • 43
  • 1
  • 9
2
votes
1 answer

How to marshal/unmarshal bson array with polymorphic struct with mongo-go-driver

I'm struggling to marshal/unmarshal bson array with polymorphic struct with mongo-go-driver。I plan to save a struct discriminator into the marshaled data, and write a custom UnmarshalBSONValue function to decode it according to the struct…
ricky
  • 2,058
  • 4
  • 23
  • 49
2
votes
1 answer

What are the high and low values in mongodb mongo-go-driver Decimal128 and how to use them to create a new decimal

The GO driver for MongoDB has a Decimal128 type which is a struct that looks like this: type Decimal128 struct { h, l uint64 } according to the docs, the h and l are high and low uint64 values. What do these two values represent? Let's say I…
Optimus Pette
  • 3,250
  • 3
  • 29
  • 50
2
votes
2 answers

mongo-go-driver logger & log level config

I don't see a way to enable logs / debugs for official mongo driver in go. mgo has SetLogger
Kamath
  • 4,461
  • 5
  • 33
  • 60
2
votes
0 answers

How to use interface type as a model in mongo-go-driver?

package code_test import ( "encoding/json" "fmt" "go.mongodb.org/mongo-driver/bson" "testing" ) type Data struct { Type int32 `bson:"Type"` IValue IGetter `bson:"IValue"` } func (m *Data) UnmarshalBSON(b []byte) error…
X-MT
  • 21
  • 3
2
votes
2 answers

Golang and MongoDB: DeleteMany with filter

I try to read and write and delete data from a Go application with the official mongodb driver for go (go.mongodb.org/mongo-driver). Here is my struct I want to use: Contact struct { ID xid.ID `json:"contact_id" bson:"contact_id"` …
Sascha
  • 10,231
  • 4
  • 41
  • 65
2
votes
1 answer

Custom UnmarshalBSON in mongo-go-driver

I would like to have some kind of a "hook" that runs whenever I get a specific type of object from the database. I thought the Unmarshaler interface is perfect for that, but... How can I implement that interface without manually unmarshalling every…
Andrew
  • 2,063
  • 3
  • 24
  • 40
2
votes
1 answer

Using FindAndModify to run queries in the official Mongo Go Driver

In the community driven Mongo driver for Go, i.e. Mgo, we can use the Apply API call to run MongoDB queries involving $set or $inc. An example of this use-case in Mgo is as follows: change := mgo.Change{ Update: bson.M{"$set":…
Arka Mukherjee
  • 2,083
  • 1
  • 13
  • 27