Thanks for input.I am able store around 30 lakhs records in within 3 min.
using below
//HeadDet having 30 lakhs records
docs := make([]interface{}, len(HeadDet))
for i := range HeadDet{
docs[i] = HeadDet[i]
}
//inserting docs into data base
collection.Insert(docs…)
But I want insert array data in the fraction of seconds instead of 3 min. Iterating array data is taking time. Is there any other way to reduce time to fraction of seconds?.
Use workers (goroutines) and separate mgo session in each (multiple connections). Look at mgo session. Copy(). Then it depends on your Mongo server how fast it is going to be.
I am trying to implement goroutine to insert data into mongo db as suggested. But it is working for 50 to 100 records. But huge records it didn’t work. Can anyone help on this?
What error message are you getting? Do you have any more information? “It does not work” is not something that helps us help you. What is not working? How is it not working?
I am not getting any error.It is inserting into MongoDB with 50 to 100 records.If i am trying with huge records it is not inserting into DB.I am using below code.
package test
import (
"os"
"gopkg.in/mgo.v2"
)
var Db *mgo.Session
type DbLink struct {
DbName string
}
func NewDbSession(url string) {
var e error
Db, e = mgo.Dial(url)
if e != nil {
panic(e)
os.Exit(-1)
}
}
func PerformInsertOperation() {
NewDbSession("localhost")
session := Db.Copy()
//defer session.Close()
col := session.DB("TEST").C("HeadDet")
ch := make(chan bool)
go func(doc []interface{}) {
col.Insert(doc...)
ch <- true
}(HeadDet)
}
Thanks. I have handled the error as well. But it is not inserting into db.No Error was throwing as well. Same code I have checked with some dummy data which would generate 50 to 100 records and same records have been inserted in DB without any issues. Only large data(3 Million) having problem.
How do you tell it’s not working then? How do you know there’s an error? Does your program indeed end and then you check your DB to see there was no data inserted? Do logs show any attempt on inserting data? etc.
The example code you presented is not complete and you are not giving a lot of information for us to help. Please give us more details and also start debugging things, throw a couple of prints here and there, maybe even use a debugger (e.g., https://github.com/derekparker/delve).
On another note, though that particular call may have not thrown an error, I’ll insist that you should always check and handle errors.