tangbinde 发表于 2017-12-15 11:27:03

MongoDB 导出、导入表

  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=&quot;host&quot;>数据库地址</param>   
  /// <param name=&quot;username&quot;>数据库用户名</param>   
  /// <param name=&quot;password&quot;>数据库密码</param>   
  /// <param name=&quot;port&quot;>数据库端口号</param>   
  /// <param name=&quot;database&quot;>数据库名称</param>   
  /// <param name=&quot;table&quot;>数据表名</param>   
  /// <param name=&quot;filename&quot;>导出的文件</param>   
  void Export(string host, string username, string password, int port, string database, string table, string filename)   
  {   
  try   
  {   
  string arguments = string.Format(@&quot;--host {0} --username {1} --password {2} --authenticationDatabase admin --port {3} --db {4} --collection {5} --out &quot;&quot;{6}&quot;&quot;&quot;,   
  host, username, password, port, database, table, filename);   
  string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @&quot;Mongo\mongoexport&quot;);
  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(&quot;Export error&quot;, ex, &quot;流量数据存储服务&quot;, &quot;Export Mongo&quot;, &quot;&quot;, new LogMsg() { Key = &quot;file&quot;, Value = filename });   
  }   
  }   
  /// <summary>   
  /// 文件导入到数据库   
  /// </summary>   
  /// <param name=&quot;host&quot;>数据库地址</param>   
  /// <param name=&quot;username&quot;>数据库用户名</param>   
  /// <param name=&quot;password&quot;>数据库密码</param>   
  /// <param name=&quot;port&quot;>数据库端口号</param>   
  /// <param name=&quot;database&quot;>数据库名称</param>   
  /// <param name=&quot;table&quot;>导入的数据表名</param>   
  /// <param name=&quot;filename&quot;>导入的文件</param>   
  void Import(string host, string username, string password, int port, string database, string table, string filename)   
  {   
  try   
  {   
  string arguments = string.Format(@&quot;--host {0} --username {1} --password {2} --authenticationDatabase admin--port {3} --db {4} --collection {5} --file &quot;&quot;{6}&quot;&quot;&quot;,   
  host, username, password, port, database, table, filename);   
  string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @&quot;Mongo\mongoimport&quot;);   
  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(&quot;Import error&quot;, ex, &quot;流量数据存储服务&quot;, &quot;Import Mongo&quot;, &quot;&quot;, new LogMsg() { Key = &quot;table&quot;, Value = table });   
  }   
  }
页: [1]
查看完整版本: MongoDB 导出、导入表