-
Notifications
You must be signed in to change notification settings - Fork 1
/
mongo-importer.go
140 lines (114 loc) · 3.52 KB
/
mongo-importer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// MongoDB to Couchbase Importer
//
// Author: Ali Hamidi <@ahamidi>
package main
import (
"fmt"
"flag"
"strings"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"github.com/couchbaselabs/go-couchbase"
)
// Source MongoDB
var mongoHost = flag.String("mhost", "127.0.0.1",
"URL of Source MongoDB Server")
var mongoPort = flag.String("mport", "27017",
"MongoDB Server Port")
var mongoUser = flag.String("muser", "",
"MongoDB Username")
var mongoPass = flag.String("mpass", "",
"MongoDB Password")
var mongoDb = flag.String("mdb", "",
"MongoDB Database")
var mongoCollection = flag.String("mcol", "",
"MongoDB Collection")
// Target Couchbase Server
var cbHost = flag.String("cbhost", "127.0.0.1",
"URL of Target Couchbase Server")
var cbPort = flag.String("cbport", "8091",
"Couchbase Server Port")
var cbBucket = flag.String("bucket", "",
"Couchbase Bucket")
var cbPass = flag.String("cbpass", "",
"Couchbase Password")
var typeField = flag.String("typeField", "collection",
"Name of Type field to be used. i.e. collection or type")
var typeName = flag.String("typeName", "",
"Type Name to be used")
func formatMongoURL(user string, pass string, host string, port string, db string) string {
s := []string{}
// Check if username/password is needed
if len(*mongoUser) > 0{
s = append(s, []string{"mongodb://", user, ":", pass, "@", host, ":", port, "/", db}...)
} else {
s = append(s, []string{"mongodb://", host, ":", port, "/", db}...)
}
url := strings.Join(s,"")
return url
}
func formatCBURL(bucket string, pass string, host string, port string) string {
s := []string{}
// Check if username/password is needed
if len(*cbPass) > 0{
s = append(s, []string{"http://", bucket, ":", pass, "@", host, ":", port, "/"}...)
} else {
s = append(s, []string{"http://", host, ":", port, "/"}...)
}
url := strings.Join(s,"")
return url
}
func main() {
// Get passed in args
flag.Parse()
mongoURL := formatMongoURL(*mongoUser, *mongoPass, *mongoHost, *mongoPort, *mongoDb)
// Connect to Source MongoDB
fmt.Println("Connecting to Source MongoDB Server...", mongoURL) // Provide feedback
session, err := mgo.Dial(mongoURL)
if err != nil {
panic(err)
} else {
fmt.Println("Connected!")
}
defer session.Close()
// Optional. Switch the session to a monotonic behavior.
session.SetMode(mgo.Strong, true)
// Connect to collection
c := session.DB(*mongoDb).C(*mongoCollection)
// Count the # of docs
count, err := c.Count()
if err != nil {
panic(err)
} else {
fmt.Println("Collection has ", count, " docs.")
}
cbURL := formatCBURL(*cbBucket, *cbPass, *cbHost, *cbPort)
// Connect to Target Couchbase Server
fmt.Println("Connecting to ", cbURL)
cb, err := couchbase.Connect(cbURL)
pool, err := cb.GetPool("default")
bucket, err := pool.GetBucket(*cbBucket)
if err != nil {
panic(err)
} else {
fmt.Println("Connected!")
}
progress := 0
//var result []struct{ Value int }
var obj bson.M
iter := c.Find(nil).Iter()
for iter.Next(&obj) {
progress++
objID := obj["_id"].(bson.ObjectId).Hex()
// Inject collection name
if *typeName == "" {
*typeName = *mongoCollection
}
obj[*typeField] = *typeName
fmt.Println(progress, "of", count, ": ", "Key: ", objID, " Value: ", obj)
bucket.Set(objID, 0, obj)
}
if err := iter.Close(); err != nil {
panic(err)
}
}