Connect Go to Mongo Atlas and find documents - mongodb

I'm a newbie to Go and trying to connect my sample application to Mongo Atlas
I`ve create a cluster in Mongo Atlas which has the following information:
I'm trying to retrieve documents from the collection but it fails to find any:
I'm able to connect to the DB, but when I run .Find() then I get an error of document is nil
When i run a specific query (for email) then it works.
When i run a general query (context.TODO()) then it is not working
What am I missing here?
const connectionStr = "mongodb+srv://" + MONGO_USER + ":" + MONGO_PASSWORD + "#cluster0.3ymvqrs.mongodb.net/?retryWrites=true&w=majority"
serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1)
clientOptions := options.Client().
ApplyURI(connectionStr).
SetServerAPIOptions(serverAPIOptions)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
log.Fatal(err, client)
}
// Select the database and collection
database := client.Database("sample_mflix")
commentsCollection := database.Collection("comments")
// Retrieve documents from the collection
// cursor, err := commentsCollection.Find(context.TODO(), nil) // not working
cursor, err := commentsCollection.Find(ctx, bson.M{"email": "roxanne_mckee#gameofthron.es"}) // working
if err != nil {
log.Fatal(err)
}
defer cursor.Close(context.TODO())

Related

How to obtain MongoDB version using Golang library?

I am using Go's MongodDB driver (https://pkg.go.dev/go.mongodb.org/mongo-driver#v1.8.0/mongo#section-documentation) and want to obtain the version of the mongoDB server deployed.
For instance, if it would been a MySQL database, I can do something like below:
db, err := sql.Open("mysql", DbUser+":"+DbPwd+"#tcp("+Host+")/"+DbName)
if err != nil {
log.Printf("Error while connecting to DB: %v", err)
}
defer db.Close()
var dbVersion string
if err := db.QueryRow("SELECT VERSION()").Scan(&dbVersion); err != nil {
dbVersion = "NA"
log.Printf("Couldnt obtain db version: %w", err)
}
fmt.Println("DB Version: ", dbVersion)
I went through the documentation but am not able to find a clue.
I also need to fetch other metadata like Size of a particular database etc.
Any help would be appreciated. Thanks!
The MongoDB version can be acquired by running a command, specifically the buildInfo command.
Using the shell, this is how you could do it:
db.runCommand({buildInfo: 1})
The result is a document whose version property holds the server version, e.g.:
{
"version" : "5.0.6",
...
}
To run commands using the official driver, use the Database.RunCommand() method.
For example:
// Connect to MongoDB and acquire a Database:
ctx := context.Background()
opts := options.Client().ApplyURI("mongodb://localhost")
client, err := mongo.Connect(ctx, opts)
if err != nil {
log.Fatalf("Failed to connect to db: %v", err)
}
defer client.Disconnect(ctx)
db := client.Database("your-db-name")
// And now run the buildInfo command:
buildInfoCmd := bson.D{bson.E{Key: "buildInfo", Value: 1}}
var buildInfoDoc bson.M
if err := db.RunCommand(ctx, buildInfoCmd).Decode(&buildInfoDoc); err != nil {
log.Printf("Failed to run buildInfo command: %v", err)
return
}
log.Println("Database version:", buildInfoDoc["version"])
Based on #icza's answer, here is how to obtain other metadata of the Database:
We need to use dbStats command to obtain metadata.
host := "<your-host-name>:<pot-number>"
url := "mongodb://" + host
credential := options.Credential{
AuthSource: "authentication-database",
Username: "username",
Password: "password",
}
clientOpts := options.Client().ApplyURI(url).SetAuth(credential)
ctx := context.Background()
client, err := mongo.Connect(ctx, clientOpts)
if err != nil {
log.Fatal("Failed to connect to db : %w", err)
}
defer client.Disconnect(ctx)
if err := client.Ping(context.TODO(), readpref.Primary()); err != nil {
panic(err)
}
fmt.Println("Successfully connected and pinged.")
db := client.Database("your-database-name")
dbStatsCmd := bson.D{bson.E{Key: "dbStats", Value: 1}}
var dbStatsDoc bson.M
if err := db.RunCommand(ctx, dbStatsCmd).Decode(&dbStatsDoc); err != nil {
log.Printf("Failed to run dbStats command: %v", err)
return
}
log.Println("\nTotal Used Size in MB: ", dbStatsDoc["totalSize"].(float64) / 1048576 , " ,Total Free size in MB (part of total used size): ", dbStatsDoc["totalFreeStorageSize"].(float64)/1048576)

How do I import a .csv file from Go to MongoDB?

I am currently using Go Mongo-driver Library. Following the instructions, I am able to insert a BSON to my local database. However, my application requires me to store a .csv file. Is there a way I can insert a .csv file to the local database.
Below is a code of how I used the mongo-driver library to insert a bson.
func main() {
log.Print("Starting the application...")
/*
Connecting to Local DB
*/
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
client, _ := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
collection := client.Database("GoMongo").Collection("Test")
res, err := collection.InsertOne(ctx, bson.D{{"name", "test"}, {"email", "astestdf#paypal.com"}})
if err != nil {
panic(err)
}
id := res.InsertedID
fmt.Print(id)
r := mux.NewRouter()
r.HandleFunc("/user", CreateUserEndPoint).Methods("POST")
http.ListenAndServe(":8080", r)
}

How to solve "command find requires authentication" using Golang and MongoDriver

I need help to fix the error. I'm using IBM mongo services.
go version go1.13.6 darwin/amd64
mongo driver version 1.2.1
The connection is working, I can read and write but sometimes it returns
: command find requires authentication and command insert requires authentication
MONGO_DB_URI=mongodb://username:password:port,host/dbname?authSource=admin&replicaSet=replset&connect=direct&alias=default
Connect:
func ConnectDatabase() *mongo.Client {
clientOptions := options.Client().ApplyURI(os.Getenv("MONGO_DB_URI"))
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
var err error
client, err = mongo.Connect(ctx, clientOptions)
if err != nil {
log.Fatal(err)
}
ctx, _ = context.WithTimeout(context.Background(), 10*time.Second)
err = client.Ping(ctx, nil)
if err != nil {
log.Fatal(err)
return nil
}
fmt.Println("Connected to MongoDB!")
return client
}
Read:
func FindAll(collectionName string, query bson.M) (*mongo.Cursor, error) {
collection := client.Database("dbname").Collection(collectionName)
singleResult, err := collection.Find(context.TODO(), query)
return singleResult, err
}
Read:
ctx, _ := context.WithTimeout(context.Background(), 20*time.Second)
cur, err := mongo.GetCollection("collection_name").Find(ctx, createQuery())
if err != nil {
log.Println(err.Error())
}
I'm using the same database and the same configurations at our another Python Project. No exceptions.
There is a difference between connecting to a DB and performing operations on the DB.
Mongo lets you connect without authentication because you have to be able to connect to be able to authenticate.
var cred options.Credential
cred.AuthSource = YourAuthSource
cred.Username = YourUserName
cred.Password = YourPassword
// set client options
clientOptions := options.Client().ApplyURI(os.Getenv("MONGO_DB_URI")).SetAuth(cred)
//... the rest of your code
Hope this helps.

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 checked the documentation but did not see any mention of returning all documents. Here is what I've tried with aforementioned result.
client, err := mongo.Connect(context.TODO(), "mongodb://localhost:27017")
coll := client.Database("test").Collection("albums")
if err != nil { fmt.Println(err) }
// we can assume we're connected...right?
fmt.Println("connected to mongodb")
var results []*Album
findOptions := options.Find()
cursor, err := coll.Find(context.TODO(), nil, findOptions)
if err != nil {
fmt.Println(err) // prints 'document is nil'
}
Also, I'm about confused about why I need to specify findOptions when I've called the Find() function on the collection (or do I not need to specify?).
Here is what I came up with using the official MongoDB driver for golang. I am using godotenv (https://github.com/joho/godotenv) to pass the database parameters.
//Find multiple documents
func FindRecords() {
err := godotenv.Load()
if err != nil {
fmt.Println(err)
}
//Get database settings from env file
//dbUser := os.Getenv("db_username")
//dbPass := os.Getenv("db_pass")
dbName := os.Getenv("db_name")
docCollection := "retailMembers"
dbHost := os.Getenv("db_host")
dbPort := os.Getenv("db_port")
dbEngine := os.Getenv("db_type")
//set client options
clientOptions := options.Client().ApplyURI("mongodb://" + dbHost + ":" + dbPort)
//connect to MongoDB
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
log.Fatal(err)
}
//check the connection
err = client.Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected to " + dbEngine)
db := client.Database(dbName).Collection(docCollection)
//find records
//pass these options to the Find method
findOptions := options.Find()
//Set the limit of the number of record to find
findOptions.SetLimit(5)
//Define an array in which you can store the decoded documents
var results []Member
//Passing the bson.D{{}} as the filter matches documents in the collection
cur, err := db.Find(context.TODO(), bson.D{{}}, findOptions)
if err !=nil {
log.Fatal(err)
}
//Finding multiple documents returns a cursor
//Iterate through the cursor allows us to decode documents one at a time
for cur.Next(context.TODO()) {
//Create a value into which the single document can be decoded
var elem Member
err := cur.Decode(&elem)
if err != nil {
log.Fatal(err)
}
results =append(results, elem)
}
if err := cur.Err(); err != nil {
log.Fatal(err)
}
//Close the cursor once finished
cur.Close(context.TODO())
fmt.Printf("Found multiple documents: %+v\n", results)
}
Try passing an empty bson.D instead of nil:
cursor, err := coll.Find(context.TODO(), bson.D{})
Also, FindOptions is optional.
Disclaimer: I've never used the official driver, but there are a few examples at https://godoc.org/go.mongodb.org/mongo-driver/mongo
Seems like their tutorial is outdated :/

How to use new URL from mongodb 3.6 to connect from golang

I tried to connect to mongodb Atlas using golang drivers.
tlsConfig := &tls.Config{}
var mongoURI = "mongodb+srv://admin:password#prefix.mongodb.net:27017/dbname"
dialInfo, err := mgo.ParseURL(mongoURI)
if err != nil {
panic(err)
}
dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
conn, err := tls.Dial("tcp", addr.String(), tlsConfig)
return conn, err
}
session, err := mgo.DialWithInfo(dialInfo)
if err != nil {
println("error")
log.Fatal(err)
}
_ = session
c := session.DB("Token").C("_Users")
user := &User{firstName: "username"}
err = c.Insert(user)
if err != nil {
println("error Again")
}
I am not getting an error not getting connected.
I am wondering what could be the reason.'
Any help is appreciated.
I tried to create DialInfo using below code
dialInfo := &mgo.DialInfo{
Addrs: []string{"prefix.mongodb.net:27017"},
Database: "dbname",
Mechanism: "SCRAM",
Timeout: 10 * time.Second,
Username: "admin",
Password: "passwrd",
}
Now I am getting no reachable servers
I could only see that the code started, then nothing
As you have figured out, this is because DialInfo by default has a zero timeout. The call will block forever waiting for a connection to be established. You can also specify a timeout with:
dialInfo.Timeout = time.Duration(30)
session, err := mgo.DialWithInfo(dialInfo)
Now I am getting no reachable servers
This is because globalsign/mgo does not currently support SRV connection string URI yet. See issues 112.
You can use the non-srv connection URI format (MongoDB v3.4), see a related question StackOverflow: 41173720.
You can use mongo-go-driver instead if you would like to connect using the SRV connection URI, for example:
mongoURI := "mongodb+srv://admin:password#prefix.mongodb.net/dbname?ssl=true&retryWrites=true"
client, err := mongo.NewClient(options.Client().ApplyURI(mongoURI))
if err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
err = client.Connect(ctx)
defer client.Disconnect(ctx)
if err != nil {
log.Fatal(err)
}
database := client.Database("go")
collection := database.Collection("atlas")
The above example is compatible with the current version v1.0.0
For MongoDB Atlas
serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1)
clientOptions := options.Client().
ApplyURI("mongodb://username:password#prefix0.mongodb.net:27017,prefix1.mongodb.net:27017,prefix2.mongodb.net:27017/?retryWrites=true&w=majority&replicaSet=atlas-zhqegh-shard-0&tls=true").
SetServerAPIOptions(serverAPIOptions)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
log.Fatal(err)
}
To clarify the MongoDB Atlas replicaSet and hosts you can for instance utilize MongoDB Compass: just connect to the cluster and you will see all that data.