0

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.

  1. How do I write these to coll_mongo ( collection_1 in db1 of mongodb)?

  2. 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 )?

4

1 回答 1

1

使用 Go first dump db docDb 使用此 bash 命令执行 bash 脚本创建 backup.sh 文件并将此命令放入其中

mongodump --db=docDb --out=./

然后使用这些命令将数据导入 mongoDb 创建 restore.sh 文件并放入其中

mongorestore -d mongoDb -c db1  ./docDb/db1.bson --drop
mongorestore -d mongoDb -c db2  ./docDb/db2.bson --drop 
mongorestore -d mongoDb -c db3  ./docDb/db3.bson --drop 

这是 Go to exec bash 命令中的示例代码

package main

import (
   "fmt"
   "exec"
   "os"
   "bytes"
   "io"
)

func main() {
    app := "bash backup.sh"
    cmd, err := exec.Run(app, []string{app, "-l"}, nil, "", exec.DevNull, exec.Pipe, exec.Pipe)

    if (err != nil) {
       fmt.Fprintln(os.Stderr, err.String())
       return
    }

    var b bytes.Buffer
    io.Copy(&b, cmd.Stdout)
    fmt.Println(b.String())

    cmd.Close()

    app := "bash restore.sh"
    cmd, err := exec.Run(app, []string{app, "-l"}, nil, "", exec.DevNull, exec.Pipe, exec.Pipe)

    if (err != nil) {
       fmt.Fprintln(os.Stderr, err.String())
       return
    }

    var b bytes.Buffer
    io.Copy(&b, cmd.Stdout)
    fmt.Println(b.String())

    cmd.Close()


}
于 2021-08-25T03:59:09.447 回答