646 questions
1
vote
1
answer
77
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":...
1
vote
0
answers
109
views
no reachable servers. MongoDB error with go using gopkg.in/mgo.v2
I am trying to establish connection with mongoDB using the below code
func getSession() \*mgo.Session {
// Connect to MongoDB on the default port
s, err := mgo.Dial("mongodb://localhost:...
2
votes
1
answer
205
views
Mongo-go driver version of session.copy()
I am migrating a go backend from using mgo to mongo-go. The service has lots of code and I have completed most of it except for a part that deals with session.Copy() method from the old mgo. The ...
2
votes
0
answers
182
views
Golang MGO driver with mongo atlas version 5 or above
Currently, most of my production is based on microservices, and the majority of the services have already been upgraded to the official MongoDB driver. However, with the end-of-life for version 4.X ...
1
vote
0
answers
62
views
golang mongodb sessionid format conversion
After linking to mongodb, the Sessionid needs to be converted back to the original format as a string
func main() {
uri := "mongodb://root:[email protected]:32082,172.22.50.25:32083,172.22.50....
3
votes
0
answers
39
views
How search with strength in mgo Golang
basically i have this query that searches from an array of names...now i want to make it case insensitive
in GO
err = c.Find(bson.M{
"name": bson.M{
"$in": ...
1
vote
0
answers
211
views
Connection from Go Application to MongoDB Is Not Persisting
I'm connecting a Go application to MongoDB using the following code:
session, sessionErr := mgo.Dial("127.0.0.1:27017")
The connection is established but doesn't persist as indicated in ...
2
votes
1
answer
2k
views
MongoDB error "no reachable servers" with golang
I just followed this tutorial on youtube(https://youtu.be/GwQ1hvuSNJA).
But I got an error "no reachable servers" after go run main.go
How can I figure it out? Should I change localhost to ...
0
votes
1
answer
685
views
MongoDb and Golang - $group and $match
I am trying to $match my data after $group but it does not work.
I have db with jobs.
Every job has {id, batchId(a few jobs can belong to one batch), createdAt, finishedAt}.
I cant understand why I am ...
3
votes
1
answer
1k
views
Avoid to save blank struct object in db
I am running the below code. When a user saved in db then blank address is saved I have assigned the null struct to address before save. I can not add the omitempty with all the fields for address ...
4
votes
0
answers
2k
views
Golang MongoDB query returns empty cursor?
I'm at a bit of an impasse here with a query that SHOULD by all accounts, return results. If I run the following query in mongosh:
db.events.aggregate([
{$unwind:"$features&...
-2
votes
1
answer
72
views
The most improved way to get all likes for each post in MGO?
Likes have different collection, so do Posts.
But, maybe it is more efficient and green that:
just getting all posts and when users hover on posts fetching likes
or when intersection api is triggered ...
3
votes
2
answers
533
views
How to store geo-spatial data in MongoDB with Go driver or mgo/qmgo?
I'm using MongoDB driver and mgo-forked qmgo in Go to operate MongoDB (version 5.x).
I have some geo-spatial data to store and query, and searched some articles and someone mentioned to create custom ...
1
vote
1
answer
1k
views
How to connect to mongoDB via ssl using .crt file in Go
I am trying to connect to a mongo database hosted in azure using the .crt file.
I am successfully able to connect from my linux machine terminal using command:
mongo mongodb://username:password@prod-...
0
votes
1
answer
451
views
How to return Mongodb Aggregate pipeline docs to ONE document?
I know this has got to be simple, but for the life of me I can't seem to generate the correct final stage in my pipeline to get this working. Here are the documents output from a stage that I have in ...
3
votes
1
answer
2k
views
Custom marshalling of BSON, type as string
I have a custom type for the purposes of formatting a time object
type MyTime time.Time
To use this with JSON I have implemented:
func (t *MyTime) UnmarshalJSON(b []byte) error
func (t MyTime) ...
1
vote
1
answer
1k
views
How do I populate a MongoDB array of foreign field IDs?
I have an array of ObjectIds in my MongoDB documents and I'd like to use Go (specifically mgo.v2) to perform a query that populates them with data from the documents that they reference. For example:
{...
0
votes
1
answer
367
views
Mongo DB query on Mongo DB driver for Golang
I need to compare two fields on my mongo db database, and this is the query for that
db.characters.find({$expr:{$eq:["$currentLv", "$maxLv"]}})
How can i query like this on mongo ...
2
votes
1
answer
834
views
mongodb query with go dynamic
I would like to build a mongo query dynamic but searching in google, i don't understand why the first query returns null.
matches := []bson.M{bson.M{"$match": bson.M{"age": bson.M{&...
0
votes
2
answers
1k
views
Get names of all keys in the collection using Go
I'd like to get the names of all the keys in a MongoDB collection.
For example, from this:
"Id": ObjectId("5f5a010d431c4519dcda0e3d")
"title": "App"...
0
votes
0
answers
836
views
How to save empty objectId in a field other than _id in mgo Golang
I am trying to save a field in the database using a struct. In this, if I initialize this struct and do no pass a valid bson id as MasterTemplateId while performing an update operation, then it gives ...
0
votes
1
answer
374
views
MongoDB query response is too slow
I am working on a Golang project (db MongoDB). I have executed the below query but it is taking too much time to load the data. In this, I am getting data from 2 collections with multiple stages.
db....
0
votes
0
answers
267
views
mgo.v2 not unmarshaling nested map in struct and instead set bson.M
Currently am working in a Go project and using mongo with gopkg.in/mgo.v2 am having issues retrieving values from the database, I have a structure like:
type person struct {
Name string
Age ...
0
votes
1
answer
271
views
Couldnt connect to mongo atlas from the mongo shell, mongo compass and go mgo Driver
I'm trying to connect mongo shell with my mongo atlas M0 cluster and ending up getting this error
Error Message
Solution I tried which didnt work:
I upgraded the monog version to 4.2 to be insync with ...
1
vote
1
answer
426
views
Match Stage on From Table in go mongodb
My mongodb collections
employee
{
_id:ObjectId(),
"emp_name":"qwert",
"emp_id":111,
"emp_dept":"XYZ"
"qualification":"PHD&...
1
vote
1
answer
1k
views
How to perform Find, Distinct & Sort all together using go mongo driver
I have a command which is made using "labix.org/v2/mgo" library
err = getCollection.Find(bson.M{}).Sort("department").Distinct("department", &listedDepartment)
this ...
-2
votes
1
answer
1k
views
Which library is More Efficient "gopkg.in/mgo.v2" or "go.mongodb.org/mongo-driver/mongo" [closed]
I am using Golang as backed language with mongodb database and also new to this language.I have used both libraries "gopkg.in/mgo.v2" and "go.mongodb.org/mongo-driver/mongo" to ...
1
vote
1
answer
189
views
how to get slice of type struct defined inside another struct using go and mongodb
mongodb collections
employee
{
_id:ObjectId(),
"emp_name":"qwert",
"emp_id":111,
"emp_dept":"XYZ"
}
{
_id:ObjectId(),
"emp_name":&...
0
votes
0
answers
48
views
Before Mongo 3.2 which method was used to find
I have an installation of Mongo v3.0.15 and I cannot execute the find command from my application. I need to build this with backward compatibility, so, which command was used instead of find for ...
1
vote
1
answer
2k
views
Golang MongoDB using $in operator on list - result argument must be a slice address
I have the following Mongo query I'm trying to translate to Go using github.com/globalsign/mgo:
db.getCollection('cluster').find({"clusterName": {"$in": ["clusterA", &...
0
votes
1
answer
464
views
Get all model tree child node in MongoDB with Go
I have data collection in MongoDB.
My data was built in model tree and relationship between child node and parent node is property parentid.
Here is my data architecture
And here is my sample data
{&...
1
vote
0
answers
554
views
Golang mgo Fetching an item by label in a nested bson array
I'm having trouble finding objects in a nested array. I need to find home/away within league array which has an events array.
Example JSON:
{
"sportId":4,
"last":266178326,
"league":[
...
1
vote
1
answer
565
views
Remove array element in MongoDB in Go
I have data in MongoDB like below:
{"_id":{"$oid":"5ed0cb4cb8d5570916d1ee7e"},"rolecode":"DHBK1_ROLE_05","productid":"XYZ_Platform","functioncodelist":["DHBK1_FUNC_1","DHBK1_FUNC_2","DHBK1_FUNC_3","...
0
votes
1
answer
882
views
Insert struct have array to Mongodb with Go
I want to insert datastruct like below to MongoDB
{
"rolecode": "DHBK1_ROLE_04",
"functioncodelist": [
"DHBK1_FUNC_1",
"DHBK1_FUNC_2",
.....
"DHBK1_FUNC_n"]
...
0
votes
1
answer
943
views
MongoDB distinct query and $in with Go
I'm having trouble with the distinct query in MongoDB.
I can write it in Mongo shell, it works but I don't know how to implement it in Go code.
Here is my Mongo shell code
db.getCollection('...
-2
votes
1
answer
200
views
Implement MongoDB shell in GO
I have data in MongoDB as tree structure model. Here is my data structure:
Here is my Mongo shell to create collection:
db.users.insert({_id:"DHBK",username:"DHBK",password:"123456",lastname:"DHBK",...
-3
votes
2
answers
348
views
Getting error while deleting the key value from array of objects
I am getting the following error while deleting from key from array of JSON objects using Go.
Error:
repository/orderRepository.go:394:11: first argument to delete must be map; have interface {}
...
1
vote
1
answer
972
views
How do I use bson.SetBSON or bson.Raw?
Possibly related: How to use interface type as a model in mgo (Go)?
I have a struct like so:
type Game struct {
ID bson.ObjectId
Type string
Location string
Details ...
1
vote
1
answer
258
views
Efficient paging in MongoDB using mgo.v2 and MongoDB > 4.2
I have already looked at Efficient paging in MongoDB using mgo and asked https://stackoverflow.com/review/low-quality-posts/25723764
I got the excelent response provided by @icza who shares his ...
0
votes
1
answer
70
views
How to fetchall records in mongodb mgo.v2?
I Want to fetch all records in database By using NAME but, if I am using ALL it is showing 500 internal error but, if I Kept One(JSON) I am getting only one record. What is the solution to fetch all ...
9
votes
1
answer
11k
views
How can I ignore duplicate key error & proceed insertion, While insert many documents with MongoDB-mgo?
When I insert multiple documents with insert(docs...), Operation is failing & fails to insert documents if one duplicate key in a document exists in list of documents. How can I ignore this error, ...
0
votes
2
answers
1k
views
updating mongo documents based in map value and remove that value
am currently working in Go and have a mongo database (connected via gopkg.in/mgo.v2) so, right now I have a data structure similar to:
{
"_id" : "some_id_bson",
"field1" : "value1",
"...
1
vote
3
answers
3k
views
Filtering values out of a bson.M in golang
I am quite new to golang and wanted to filter name and version for packages out of a response from my mongodb. I am using mgo to get the respone from the mongodb.
This is my Code to get the response ...
0
votes
1
answer
73
views
Find all elements sorted by best matches
I have some entities in my database called "events". Each of these events contain an array of string, called "tags".
I want to make a query to get all the events matching an array of tags that I will ...
0
votes
1
answer
763
views
how to use $facet in golang with library mgo?
I started learning performance improvements on mongoDb. And i have a question based aggregation functions.
I have created a basic test collection with 3 fields product, colour and year:
{
"...
1
vote
1
answer
203
views
Mgo Aggregate pipline $not operator. Unknown top level operator
I'm trying to create an aggregate pipeline using the Mgo driver. I keep running into the unknown top level operator $not issue for the following setup. I'm a beginner at using go and mongo. I'm ...
1
vote
1
answer
119
views
How to retrieve a mongo document with an array of objects that have matching attribute
I have a collection of user documents, each with an embedded array of clients. Each object in the array has a clientID.
I wanted to retrieve the user document with a client, in the clients array, ...
0
votes
1
answer
495
views
Connect from Go to remote Linux MongoDb (no auth)
Upon logging in to Linux box, no authentication is required to connect with MongoDB.
How to connect from Go code to such MongoDB instance? I need to SSH into Linux box using username and password, ...
3
votes
1
answer
2k
views
mgo is setting objectid to objectidhex, which doesnt seem to get read by Mongodb
I am trying to make a query using the ObjectId, and normally in mongodb you would do something like this
db.collection.findOne({"_id":objectid("5d9d90e5ed645489aae6df64")})
Which this works when I do ...
1
vote
1
answer
819
views
Converting Mongodb query to mgo
I'm trying to do a query in mongo that basically will be...Get all documents that match this instance Id AND where the status does not equal Deleted or Rejected. I figured out how to do this in ...