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.…
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
…
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…
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…
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…
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"…
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…
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…
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 (
…
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…
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
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!
…
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 …
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…
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) {
…