Go & Mongo: Install Mongodb Driver
Go & Mongo: Install Mongodb Driver
Checkout this quick walkthrough and create your first free cluster.
● Initialize Go modules
go get go.mongodb.org/mongo-driver
Though you can allow access from any IP but it’s safe to
whitelist your IP so that anyone cannot access your
connection. check this page to know more.
So Now we have everything ready in MongoDB Atlas. Now it’s time to jump into coding.
Add the following Packages. Please checkout the package details from this page
import (
"context"
"fmt"
"log"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
func ConnectToAtlas(){
client, err := mongo.NewClient(options.Client().ApplyURI("the connection string yo
if err != nil {
log.Fatal(err)
}
package main
import (
"fmt"
)
func main(){
fmt.Println("connecting to the server....")
ConnectToAtlas()
}