I have two database connections docDb
and mongoDb
each having three databases db1
,db2
, and db3
. I want to read all data from db1
of docDb
to db1
of mongoDb
in Go using mongo-driver
and I want to do the same for other databases as well.
This is what I have so far,
func MigrateData(mongoConnection *mongo.Client, docDbConnection *mongo.Client){
db1Doc := docDbConnection.Database("db1")
db1Mongo := mongoConnection.Database("db1")
coll_doc := db1Doc.Collection("collection_1")
cursor, err := coll_doc.Find(context.TODO(), bson.M{})
if err != nil {
log.Fatal(err)
}
var documents []bson.M
if err = cursor.All(context.TODO(), &documents); err != nil {
log.Fatal(err)
}
coll_mongo := db1Mongo.Collection("collection_1")
}
Now this documents
consists of all the documents in collection_1
in db1
. But I am not able to figure out how to write these to collection_1
of db1
and skip if the document is already present. I did some research online but all I found was inserting custom-made document objects. But here I am getting type []primitive.M
.
How do I write these to
coll_mongo
(collection_1
indb1
ofmongodb
)?If I do it this way, I will need to know the collection names beforehand and migrate in between collections, or get a list of collection names and loop through it but this sounds like a workaround or a wrong way to do it. Is there another way in Go and
mongo-driver
where I can read everything from a database and directly write it to another database ( all collections, all documents )?