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)
}
Related
This question already has answers here:
Is mongodb client driver concurrent safe?
(1 answer)
goroutine create multiple mongodb connection
(1 answer)
Closed 21 days ago.
I've managed to connect to MongoDB and I've got this client object, can this be passed around and reused?
Or does it need to be created each time I want to interact with the database?
If it can be reused then ill move the database connection out to its own function and run it just once at startup.
package main
import (
"context"
"fmt"
"log"
"main/api"
"os"
"time"
"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type User struct {
Username string
Password string `bson:"password,omitempty"`
}
func main() {
if err := godotenv.Load(); err != nil {
log.Println("No .env file found")
}
uri := os.Getenv("MONGODB_URI")
serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1)
clientOptions := options.Client().
ApplyURI(uri).
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)
}
usersColl := client.Database("sampleShop").Collection("users")
newUser := User{Username: "8282", Password: "Korean"}
result, err := usersColl.InsertOne(context.TODO(), newUser)
if err != nil {
panic(err)
}
fmt.Println(result)
fmt.Println(uri)
api.GetPaymentSession()
}
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())
I'm trying to get 10000 document at a time in mongodb, but i got :
Information :
Driver https://github.com/mongodb/mongo-go-driver
opt.SetBatchSize(15_000)
opt.SetAllowPartialResults(false)
index on timestamp
Code :
package main
import (
"context"
"fmt"
"net/http"
"os"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var database *mongo.Database
func main() {
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://20.20.20.43:27017"))
if err != nil {
panic(err)
}
database = client.Database("chat_data")
chatText := make([]chat, 0)
now := time.Now().Unix()
ctx, _ = context.WithTimeout(context.Background(), 30*time.Second)
// mongodb batch option
opt := options.Find()
opt.SetBatchSize(15_000)
opt.SetAllowPartialResults(false)
// mongodb filter
filter := bson.M{"timestamp": bson.M{"$gte": now - 108000}}
cur, err := database.Collection("chat").Find(ctx, filter, opt)
if err != nil {
// fmt.Fprint(w, err)
fmt.Println(err)
return
}
defer cur.Close(ctx)
for cur.Next(ctx) {
var result chat
err := cur.Decode(&result)
if err != nil {
fmt.Println(err)
continue
}
// do something with result....
// fmt.Println(result)
chatText = append(chatText, result)
}
if err := cur.Err(); err != nil {
// fmt.Fprint(w, cur.Err())
fmt.Println(err)
return
}
fmt.Println("done")
fmt.Println(len(chatText))
}
can i achieve this with mongodb & go driver ?, 30 second timeout are always reached
Edit 1
i try in python (with pymongo) it's only need 0m2.159s to query 36k doc with that filter
Try 7000, if it works try 12000, if it doesn't work try 4000, etc.
Make note of how long these requests take to figure out if the execution time is proportional to batch size.
You are querying on just the timestamp field. If you create an index on that collection with the timestamp field first, you should get faster results, and get a free sort in the process.
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.
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.