Last active
December 14, 2017 13:21
-
-
Save freekman/1fb1cd599e83a056a94090c8c3902959 to your computer and use it in GitHub Desktop.
Mongo file (csv) migration script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"bufio" | |
"os" | |
"path/filepath" | |
"strings" | |
"gopkg.in/mgo.v2" | |
"time" | |
"strconv" | |
) | |
func main() { | |
absPath, _ := filepath.Abs("./namedays.csv") | |
file, err := os.Open(absPath) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
defer file.Close() | |
scanner := bufio.NewScanner(file) | |
scanner.Split(bufio.ScanLines) | |
for scanner.Scan() { | |
//1,1,"Васил, Василен, Василена ..." | |
data := scanner.Text() | |
parts := strings.Split(data,",") | |
if len(parts)<3{ | |
continue | |
} | |
day:=parts[0] | |
month :=parts[1] | |
names:= parts[2:] | |
for i,v:= range names { | |
names[i] = strings.Replace(v, `"`,``,-1) | |
} | |
fmt.Println(day) | |
fmt.Println(month) | |
fmt.Println(names) | |
mongoDBDialInfo := &mgo.DialInfo{ | |
Addrs: []string{"127.0.0.1:27017"}, | |
Direct: false, | |
} | |
session, err := mgo.DialWithInfo(mongoDBDialInfo) | |
if err != nil { | |
fmt.Println("Cant connect to MONGO ......") | |
return | |
} | |
d,_:=strconv.Atoi(day) | |
m,_:=strconv.Atoi(month) | |
date:=int64(time.Date(time.Now().Year(), time.Month(m), d,0,0,0,0, time.UTC).UTC().Unix()*1000) | |
nd:=NameDay{ | |
TimeStamp:date, | |
Names:names, | |
} | |
session.DB("taxime_test").C("name_days").Insert(&nd) | |
} | |
} | |
type NameDay struct { | |
TimeStamp int64 | |
Names []string | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment