const params = {
connection: "ip地址",
db: "数据库名称",
type: "sql",
batchSize: 2000,
exportCollections: [
{
collection: "table",
fields: [
{
sourceField: "_id",//MongoDb中的原字段
targetField: "MongoDBId",//导出至sql中的字段
dataType: "VARCHAR(255)",//类型
bsonType: "ObjectId",
nullable: false
},
{
sourceField: "Name",
targetField: "SqlName",
dataType: "VARCHAR(255)",
bsonType: "String",
nullable: true
}
],
table: "#MongoTmp",//导出至sql中的表名称
filepath: "导出文件的路径.sql",
dialect: "MSSQL",
withTableStructure: false
}
]
};
mb.exportCollections(params);
sleep(100);
mb.openFolder(require("path").dirname(params.exportCollections[0].filepath));
如超过1000条无法直接全部导出,则新建一个临时表,分批将临时表的数据插入到临时表中,再进行导出,导出条件通过_id进行筛选。
// 获取1000条数据插入到临时表中
db.table.find({})
.sort({_id:-1})
.limit(1000)
.skip(1000)
.forEach(
function(doc){
db.tempTable.insertOne({"_id":doc._id,"Name":doc.Name})
}
)
// 导出至sql
...
// 删除
db.getSiblingDB("dbName").getCollection("tempTable").deleteMany({});