All Questions
1,778 questions
1
vote
1
answer
60
views
How to partially override decoding behavior using Mongo Golang Driver v2
The timeCodec in the Mongo driver throws an error when decoding doubles into time.Time fields. I'm dealing with legacy data that stores time as a double and it's impractical for me to migrate ...
1
vote
1
answer
99
views
MongoDB Go Driver v2: How to Marshal a Nil Custom Type as BSON Null?
I’m using MongoDB GoLang Driver v2 and I want to store a custom money type in MongoDB.
If the value is nil, I want the BSON document to store price: null.
Example document I want:
{
"name":...
3
votes
2
answers
157
views
MongoDB Go driver’s topology view is stale and reports no primary indefinitely
I'm encountering an issue in a Go application that uses the official MongoDB Go driver. We have the use case where a ReplicaSet is recreated from scratch. Replica Set consists of 2 members, no arbiter ...
-1
votes
1
answer
90
views
Using true/false vs 1/0 in MongoDB Projections — Long-Term Compatibility [closed]
In our Go codebase, we’re building dynamic projection maps for queries. Instead of using map[string]int{"name": 1}, we’re considering using map[string]bool{"name": true} for ...
1
vote
2
answers
80
views
I cannot query document using ObjectID
When I try to query to collection using ObjectID, I got no documents.
objID, err := primitive.ObjectIDFromHex("6812a288d8169501c71c69b2")
if err != nil {
return domain.User{}, ...
0
votes
2
answers
113
views
mocking mongo transactions in golang
I have been running into some issues when writing a few units tests. When the method has a transaction it just hangs and eventually times outs. I have been trying to figure how I can mock the ...
1
vote
1
answer
75
views
How can I reduce the response time of Mongo Go Driver when getting concurrent requests?
I have built a REST API that have a Mongo database to persist data. I ran a simple load test to measure how the API behaves to 100 requests per seconds in a infinite period of time.
As the test goes I ...
2
votes
1
answer
83
views
Saving a slice of strings to MongoDB
I am trying to add a userID to a likes field in MongoDB. This field will be an array of strings []string . It is currently null, but that should not be an issue since was using "$set". I ...
2
votes
1
answer
95
views
Cannot parse BSON datetime from string
I'm parsing BSON from strings using the mongo driver library.
AFAIK, the following should be valid BSON:
{
"createdAt": {"$date": "2024-01-01T00:00:00.000Z"}
}
but the ...
1
vote
0
answers
34
views
Run non-BSON formatted mongo aggregation pipelines with go
I have a small go utility that lets me specify a connection and databases and an aggregation file that lets me run the aggregation pipeline against the specified databases.
The heart of it is ...
2
votes
1
answer
192
views
How to Efficiently Manage MongoDB Index Creation in a Go Application?
I'm building a Go application deployed on Kubernetes that uses MongoDB as the database. Currently, my application includes logic to create indexes on collections during every pod startup. While this ...
1
vote
1
answer
63
views
How to convert double to a time.Time value with the mongo Golang driver?
I've got a database with some legacy data where some of the documents store the field "createdDate" as a normal bson date, but others store the field as an epoch millisecond timestamp with ...
1
vote
0
answers
54
views
MongoDB ObjectID binding and validation in Golang
I want an use case that receive a list of string (represent for ObjectID hex) and I want to build a struct using binding tag to validate:
If receiving one, we could use:
type SingleID struct {
...
1
vote
1
answer
1k
views
How to mock mongodb driver in golang
In my Golang project I need to mock mongodb to be able to write unit tests for my Repository functions. I mostly need to mock collection.Indexes.CreateMany or FindOneAndUpdateor Find or cursor.All or ...
1
vote
1
answer
94
views
Server selection timeout with MongoDB and Docker in Digital Ocean App Platform
I'm experiencing an issue with MongoDB and Docker when deploying to Digital Ocean App Platform from Docker Hub.
Here is the error output from the console:
server selection error: server selection ...