MongoDB Export, Import table

Source: Internet
Author: User
Tags mongoclient

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.