var srctbl = "20161226";
var desttbl = "20161226-bak";
Way One:
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
});
Way two:
Required files in the MONGO directory:
Libeay32.dll
Mongoexport.exe
Mongoimport.exe
Ssleay32.dll
<summary>
Exporting a database to a file
</summary>
<param name= "Host" > Database address </param>
<param name= "username" > Database user name </param>
<param name= "password" > Database password </param>
<param name= "Port" > Database port number </param>
<param name= "Database" > DB name </param>
<param name= "table" > Data table name </param>
<param name= "filename" > exported file </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}--ou T "" {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, "Traffic data storage Service", "Export Mongo", "" ", New Logmsg () {Key =" file ", Value = filename});
}
}
<summary>
File Import to Database
</summary>
<param name= "Host" > Database address </param>
<param name= "username" > Database user name </param>
<param name= "password" > Database password </param>
<param name= "Port" > Database port number </param>
<param name= "Database" > DB name </param>
<param name= "table" > Imported data table name </param>
<param name= "filename" > imported file </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}--f Ile "" {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, "Traffic data storage Service", "Import Mongo", "" ", New Logmsg () {Key =" table ", Value = table});
}
}
MongoDB Export, Import table