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
3
votes
0 answers

Go-Mongo One to Many Relationship

How to manage relationship in go-mongo? I am trying as follows but not working: type Post struct { ID primitive.ObjectID `json:"id" bson:"_id"` Text string `json:"text" bson:"text,omitempty"` Comments …
Zayn Korai
  • 493
  • 1
  • 6
  • 24
3
votes
1 answer

Getting all data from mongodb compound collection with filter in golang

I try to make get all data with Name field where I specify in API Request Body. I made a filter for .Find() function. But I can't get any result (response body says null, No errors at all). You can see my model file and other parts of the code at…
Poyraz HANCILAR
  • 409
  • 1
  • 4
  • 12
3
votes
1 answer

Check zero Equivalent of time.Time when retrieving from mongodb Golang

So I have a very Simple Struct which is persisted in the MongoDB type Test struct { ID string `bson:"_id"` Status string `bson:"status"` TestTime …
EagerLearner
  • 447
  • 1
  • 5
  • 11
3
votes
1 answer

How to convert the following aggregate query to bson and execute in golang

This is the following query that I want to run it from…
Sri Chak
  • 85
  • 6
3
votes
1 answer

Dump a mongodb array of objects in a slice of struct

I have an array of objects along with other fields in a mongodb document: db.config.find({},{list_attributes:1, _id:0}); [ { list_attributes: { '0': { field: 'LASTNAME', field_key: 'lastname', dataType: 'text' }, '1': { field:…
Arun Suryan
  • 1,615
  • 1
  • 9
  • 27
3
votes
1 answer

Using mongo in golang, Expire Documents after a Specified Number of Seconds?

I am trying something simple using the mongo-go-driver. I insert some datas in a collection, and I want them to be automaticaly deleted after a number of seconds. I have read the following documentation :…
yayou
  • 41
  • 6
3
votes
1 answer

How to find result with mongo-driver by objectid field

I have 2 structs: type Customer struct { MyID int `bson:"myID"` } type Bag struct { Customer primitive.ObjectID `bson:"customer"` } But I dunno how to make filter to find document in Bag collection by Customer.MyID I tried something like…
stolichna9
  • 31
  • 1
3
votes
1 answer

Nested field update using golang struct in mongoDB

I am facing a issue with update document using golang mongo driver. Scenario: I want to update a field that is nested in a struct. For ex: StructOuter -> structInner -> field1, field2, field3. Now if I want to update the field3 and I have the…
3
votes
1 answer

mongo-go-driver: nested OR/AND query filter

I try to create a MongoDB query filter with the nested operators (OR/AND/...). But lib requires to create a bson.D and pass bson.E elements into it. If I need to have OR/AND inside AND/OR - I need to put bson.M + bson.D inside bson.D like…
Alexey
  • 2,582
  • 3
  • 13
  • 31
3
votes
1 answer

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 a normal query but in go lang it gives it the…
Malikiah
  • 149
  • 2
  • 2
  • 13
3
votes
1 answer

cannot decode array into an ObjectID

I have this struct and when I decode it from the database to struct I'm getting this error cannot decode array into an ObjectID type Student struct { ID primitive.ObjectID `bson:"_id,omitempty"` ... Hitches …
OhhhThatVarun
  • 3,981
  • 2
  • 26
  • 49
3
votes
1 answer

MongoDB list databases with given prefix in Go

Question How can I list databases only with the given prefix (prefix_)? Example: package main import ( "context" "fmt" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" …
FL3SH
  • 2,996
  • 1
  • 17
  • 25
3
votes
1 answer

How to perform addToSet using Go official driver?

I need to do addToSet operation using official Go MongoDB driver. In MongoDB we have some docs: { _id: 2, item: "cable", tags: [ "electronics", "supplies" ] } Then to perform addToSet: db.inventory.update( { _id: 2 }, { $addToSet: { tags: {…
Bijak Antusias Sufi
  • 198
  • 2
  • 5
  • 18
3
votes
2 answers

Mongo-go how to use arrayFilter to find elem in "array of objects inside array of objects"

Lets imagine a JSON like: "user": { "id": "1234", ...some fields, "achievements": [ { "scope": "life achievements", "list": [ {"_id": 1, "title": "some text", "gotAt": "some date"}, {"_id": 2, "title": "some…
Sergey Shopin
  • 63
  • 2
  • 8
3
votes
1 answer

mongo-go-driver fails with Server Selection Timeout when using MongoDB Atlas

Go Version: 1.12.5 I have this code which uses the node.js mongo driver const MongoClient = require('mongodb').MongoClient; const uri = process.env.MONGO_HOST + "dbname?retryWrites=true"; const client = new MongoClient(uri, { useNewUrlParser:…
Ayush Gupta
  • 8,716
  • 8
  • 59
  • 92
1 2
3
15 16