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
23
votes
4 answers

Primitive.ObjectID to string in Golang

I am trying to convert type primitive.ObjectID to string type in Go. I am using mongo-driver from go.mongodb.org/mongo-driver. I tried using type assertion like- mongoId := mongoDoc["_id"]; stringObjectID := mongoId.(string) Which VSCode accepts.…
Rabi
  • 359
  • 1
  • 2
  • 7
17
votes
1 answer

Mongo-go-driver get objectID from insert result

After using InsertOne to create a new document, when I return the result I'm getting an array of numbers rather than an ObjectID. In the DB, the id is generating fine. type User struct { ID string Email string Username string …
kue
  • 341
  • 1
  • 4
  • 11
16
votes
2 answers

bson.D vs bson.M for find queries

This specifc question is in relation to using mongodb with the golang package mongo-driver, but I would assume this applies across most interfaces with mongodb. When using Find to query some data from a collection, we can use both the bson.M- and…
abcalphabet
  • 1,158
  • 3
  • 16
  • 31
12
votes
3 answers

With mongodb-go-driver how do I get the inner exceptions

When I insert into a collection with the new MongoDB Go driver I get a duplicate exception that I can see when spewing it. (mongo.WriteException) multiple write errors: [{write errors: [{E11000 duplicate key error collection: fhir.documents…
Donald French
  • 1,731
  • 1
  • 17
  • 30
12
votes
2 answers

Find entries via substring regex query in mongodb-go-driver

I can't get the official go mongo driver to successfully return objects that are queried via a regex query. I already know how to do it via the mongo shell and get my expected results. With this example i get all entries that contain "he" in their…
Fyreek
  • 145
  • 2
  • 7
12
votes
1 answer

mongodb-go-driver/bson struct to bson.Document encoding

I'm working with https://github.com/mongodb/mongo-go-driver and currently trying to implement a partial update of such struct type NoteUpdate struct { ID string `json:"id,omitempty" bson:"_id,omitempty"` Title string `json:"title"…
8
votes
2 answers

Find all documents in a collection with mongo go driver

I checked out the answer here but this uses the old and unmaintained mgo. How can I find all documents in a collection using the mongo-go-driver? I tried passing a nil filter, but this does not return any documents and instead returns nil. I also…
Kelly Flet
  • 514
  • 2
  • 5
  • 23
8
votes
1 answer

How to filter fields from a mongo document with the official mongo-go-driver

How can I filter fields with the mongo-go-driver. Tried it with findopt.Projection but no success. type fields struct { _id int16 } s := bson.NewDocument() filter := bson.NewDocument(bson.EC.ObjectID("_id", starterId)) var opts…
Karl Wolffgang
  • 83
  • 1
  • 1
  • 4
8
votes
3 answers

Using mongodb go driver for decoding documents into structs with custom type fields

I'm a beginner in both go and mongodb. I try to decode a DocumentResult into a struct using bson tags, and it does not work for a custom type wrapping a string. Can it be done without changing the field's type to a string? import ( …
amz
  • 83
  • 1
  • 1
  • 5
7
votes
3 answers

Mocking MongoDB response in Go

I'm fetching a document from MongoDB and passing it into function transform, e.g. var doc map[string]interface{} err := collection.FindOne(context.TODO(), filter).Decode(&doc) result := transform(doc) I want to write unit tests for transform, but…
user10400458
6
votes
1 answer

String to Primitive.ObjectID

I am using mongo-driver from go.mongodb.org/mongo-driver. I already converted primitive.ObjectID to string Using this link Primitive.ObjectID to string Now i need to convert string to primitive.ObjectID
Faizal Ahamed
  • 118
  • 1
  • 4
  • 7
6
votes
0 answers

Golang mongodb build reference relationship

I have this one-to-many reference relationship in graphql with Mongo go driver. type User { id: ObjectID! email: String! username: String! posts: [Post!]! } type Post { id: ObjectID! author: User! title: String! …
The questioner
  • 724
  • 10
  • 21
6
votes
4 answers

How to get ObjectID as String from mongo-go-driver cursor?

I would like to get ObjectID as a string because I have other storage types, so I want to avoid to use primitive.ObjectID in my struct to keep the layers independent. I'm new to Golang, thanks. package listing type Card struct { ID …
Bruno Canongia
  • 514
  • 6
  • 12
6
votes
3 answers

How to ignore nulls while unmarshalling a MongoDB document?

I would like to know if there's any approach that would allow me to ignore null types while unmarshalling a MongoDB document into a Go struct. Right now I have some auto-generate Go structs, something like this: type User struct { Name string…
Andrew
  • 63
  • 1
  • 3
6
votes
1 answer

How can I convert my mgo sessions to mongo-go-driver clients using connection pooling?

Long, long ago, when we were using mgo.v2, we created some wrapper functions that copied the session, set the read pref and returned that for consumption by other libraries, e.g. func NewMonotonicConnection() (conn *Connection, success bool) { …
TopherGopher
  • 655
  • 11
  • 21
1
2 3
15 16