MongoDB 匯出、匯入表

來源:互聯網
上載者:User

標籤:ram   export   connect   auth   size   format   cat   word   ring   

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 });
            }
        }

MongoDB 匯出、匯入表

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.