博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MongoDB 导出、导入表
阅读量:5840 次
发布时间:2019-06-18

本文共 2869 字,大约阅读时间需要 9 分钟。

var srcTbl = "20161226";

var destTbl = "20161226-bak";

 

方式一:

MongoClient client;
MongoServer server;
MongoDatabase db;

MongoClientSettings setting = new MongoClientSettings();

setting.MaxConnectionPoolSize = 1000;
setting.MinConnectionPoolSize = 500;

client = new MongoClient(mongodb);

server = client.GetServer();

db = server.GetDatabase(database);

db.CreateCollection(destTbl);

db.GetCollection(srcTbl).FindAll().Foreach(doc =>
{
      db.GetCollection(destTbl).Insert(doc); // start to replace
});

方式二:

Mongo目录下需文件:

libeay32.dll

mongoexport.exe

mongoimport.exe

ssleay32.dll

/// <summary>

        /// 导出数据库到文件
        /// </summary>
        /// <param name="host">数据库地址</param>
        /// <param name="username">数据库用户名</param>
        /// <param name="password">数据库密码</param>
        /// <param name="port">数据库端口号</param>
        /// <param name="database">数据库名称</param>
        /// <param name="table">数据表名</param>
        /// <param name="filename">导出的文件</param>
        void Export(string host, string username, string password, int port, string database, string table, string filename)
        {
            try
            {
                string arguments = string.Format(@"--host {0} --username {1} --password {2} --authenticationDatabase admin --port {3} --db {4} --collection {5} --out ""{6}""",
                    host, username, password, port, database, table, filename);
                string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Mongo\mongoexport");

                Process p = new Process();

                ProcessStartInfo startInfo = new ProcessStartInfo
                {
                    FileName = fileName,
                    Arguments = arguments,
                    CreateNoWindow = true,
                    UseShellExecute = false
                };
                p.StartInfo = startInfo;
                p.Start();
                p.WaitForExit();
            }
            catch (Exception ex)
            {
                log.Error("Export error", ex, "流量数据存储服务", "Export Mongo", "", new LogMsg() { Key = "file", Value = filename });
            }
        }
        /// <summary>
        /// 文件导入到数据库
        /// </summary>
        /// <param name="host">数据库地址</param>
        /// <param name="username">数据库用户名</param>
        /// <param name="password">数据库密码</param>
        /// <param name="port">数据库端口号</param>
        /// <param name="database">数据库名称</param>
        /// <param name="table">导入的数据表名</param>
        /// <param name="filename">导入的文件</param>
        void Import(string host, string username, string password, int port, string database, string table, string filename)
        {
            try
            {
                string arguments = string.Format(@"--host {0} --username {1} --password {2} --authenticationDatabase admin  --port {3} --db {4} --collection {5} --file ""{6}""",
                    host, username, password, port, database, table, filename);
                string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Mongo\mongoimport");
                Process p = new Process();
                ProcessStartInfo startInfo = new ProcessStartInfo
                {
                    FileName = fileName,
                    Arguments = arguments,
                    CreateNoWindow = true,
                    UseShellExecute = false
                };
                p.StartInfo = startInfo;
                p.Start();
                p.WaitForExit();
            }
            catch (Exception ex)
            {
                log.Error("Import error", ex, "流量数据存储服务", "Import Mongo", "", new LogMsg() { Key = "table", Value = table });
            }
        }

转载于:https://www.cnblogs.com/94cool/p/6229301.html

你可能感兴趣的文章
mysql root密码重置
查看>>
33蛇形填数
查看>>
选择排序
查看>>
SQL Server 数据库的数据和日志空间信息
查看>>
前端基础之JavaScript
查看>>
自己动手做个智能小车(6)
查看>>
自己遇到的,曾未知道的知识点
查看>>
P1382 楼房 set用法小结
查看>>
分类器性能度量
查看>>
docker 基础
查看>>
写一个bat文件,删除文件名符合特定规则,且更改日期在某
查看>>
我的友情链接
查看>>
写Use Case的一种方式,从oracle的tutorial抄来的
查看>>
【C#】protected 变量类型
查看>>
Ubuntu解压
查看>>
爬虫_房多多(设置随机数反爬)
查看>>
藏地密码
查看>>
爬虫去重(只是讲了去重的策略,没有具体讲实现过程,反正就是云里雾里)...
查看>>
react中将px转化为rem或者vw
查看>>
8816
查看>>