MongoDB single table data export and recovery examples

  • 2020-06-01 11:14:29
  • OfStack

MongoDB is a cross-platform, document-oriented database that provides high performance, high availability, and scalability. MongoDB works on the concept of collection and documentation.

The database

The database is a collection of physical containers. Each database has its own set of files on the file system. A single 1 MongoDB server usually has multiple databases.

A collection of

The collection is a set of MongoDB documents. It is equivalent to one RDBMS table. Collect a database that exists in a single one. Collections do not execute patterns. Documents within a collection can have different domains. Typically, all files in a collection are for the same or related purpose.

The document

The document is a set of key-value pairs. File dynamic mode. Dynamic mode refers to documents in the same collection that do not require a collection of common fields with the same fields or structural groups and can hold different types of data.

1. Export of single table data

MongoDB provides the mongoexport command for exporting single-table data. mongoexport can export data to either CSV or JSON files. The difference between the two is:

JSON is the default export format of mongoexport, which need not be specified. The export CSV format must explicitly specify each property name, while the export JSON format does not. Therefore, it is more flexible and convenient to export JSON format, and there is no risk of losing attributes for complex table structures. Therefore, the author prefers this export format. This is the format used for the examples in this article.

trans.sp table structure is as follows:

"_id" : ObjectId("56ea7cc7ec8d83257c013844"),
"orderNum" : "804918166317174541239",
"sysOrderNum" : "43fd399715df4e874c773373c422a896",
"respCode" : "09",
"merId" : "991653448160401",
"transAmt" : NumberLong(300),
"transStatus" : "10",
"transType" : NumberInt(1),
"chanMerId" : "1244891002",
"chanCode" : "WXP",
"createTime" : "2016-03-17 17:45:43",
"updateTime" : "2016-03-17 17:45:43",
"refundStatus" : NumberInt(0),
"fee" : NumberLong(2),
"netFee" : NumberLong(2),
"tradeFrom" : "openapi",
"lockFlag" : NumberInt(0),
"settRole" : "WXP",
"currency" : "CNY",
"busicd" : "PAUT",
"agentCode" : "25911283",
"qrCode" : "weixin://wxpay/bizpayurl?pr=lh73Vjt",
"terminalid" : "802918",
"errorDetail" : " In the processing ",
"goodsInfo" : " Make fun, 85g,3.0,1",
"discountAmt" : NumberLong(0),
"merName" : " Cloud high-tech service co. LTD ",
"agentName" : " Baishan institutions "

Export the table using mongoexport:

$ mongoexport --host localhost --port 27017 --username quicktest --password quicktest --collection trans.sp --db quicktest --out trans.sp.json
2016-03-14T14:23:36.985+0800 connected to: localhost:27017
2016-03-14T14:29:28.085+0800 exported 2823382 records

The size of trans.sp.json is 2.6 G, with a total of 2008.1 million records. The export took 6 minutes. Specific description of each parameter:
-- localhost after host: to export ip--port after 27017: port number of the instance node to export -- quicktest after username: database user name --password after collection: trans after collection.sp: table name to export -- quicktest after db: The database name of the table to be exported -- trans.sp.json after out: the file path to be exported (default is the current folder)

2. Single table data import

To borrow and to repay, MongoDB provides the command mongoimport corresponding to the mongoexport command. Here is the import command:
$ mongoimport --host localhost --port 27019 --username quicktest --password quicktest --collection trans.sp --db quicktest --file /opt/kdf/trans.sp.json
2016-03-17T18:15:33.909+0800 connected to: localhost:27019
2016-03-17T18:15:36.888+0800 [........................] quicktest.trans.sp 1
5.0 MB/2.5 GB (0.6%)

Specific description of each parameter:

-- localhost after host: ip after port: 27019 after port: port number of the instance node to be imported -- quicktest after username: quicktest after password: trans after collection To import the table of the database name - after file/opt kdf/trans sp. json: you want to import the source file path (the default is the current folder) it's as simple as that.
But here are a few things to note:

Before the import of mongoimport, no sample of trans.sp table 1 can be imported successfully in the new database. The input sequence of each parameter required by mongoexport and mongoimport is not strict; For replica sets, the data export of mongoexport can be either an primary node or an secondary node in the replica set. For replica sets, the data import for mongoimport must be an primary node; For large data export, there is no production problem with mongoexport. For large data imports (over 500 MB), it is very easy to bring down the primary node with the mongoimport operation, as this will cause the master node to crash due to full synchronization, so avoid large data imports in the production environment. The correct approach is to set up the single point, import, and then set up the replica set. The MongoDB version number used in this article's example is 3.2.1.

About MongoDB single table data export and recovery examples to explain the knowledge to introduce you here, I hope to help you!

Related articles: