MongoDB single table data export and recovery example explanation _mongodb

Source: Internet
Author: User
Tags mongodb mongodb server mongodb version port number

MongoDB is a cross-platform, document-oriented database that delivers high performance, high availability, and scalability. MongoDB work in the collection and document concept.

Database

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

Collection

A collection is a set of MongoDB documents. It is equivalent to an RDBMS table. The collection exists in a single database. The collection does not perform a pattern. The documents within the collection can have different fields. Typically, all files in a collection are of the same or related purpose.

Document

A document is a set of key-value pairs. File dynamic mode. Dynamic mode means that documents in the same collection do not need a collection of common fields of the same field or structure group to hold different types of data.

1. Export of single table data

MongoDB provides the Mongoexport command for export of one-table data. Mongoexport can either export data to a CSV-formatted file or export a JSON-formatted file. The difference between the two is:

JSON is a mongoexport default export format that you do not need to specify, and you need to explicitly specify that you want to export a CSV format; You must explicitly specify each property name in the export CSV format, but you do not need to export the JSON format. This shows that the export JSON format is more flexible and convenient, and there is no risk of property loss for the complex table structure, so I prefer this export format. This is the format for this example.

The TRANS.SP table structure is as follows:

{
"_id": ObjectId ("56ea7cc7ec8d83257c013844"),
"Ordernum": "804918166317174541239",
"Sysordernum": " 43fd399715df4e874c773373c422a896 ",
" Respcode ":" ","
Merid ":" 991653448160401 ",
" Transamt ": Numberlong (+),
"Transstatus": "Ten",
"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 ":" "
goodsinfo": "Shuangshan fun son 85g,3.0,1",
"Discountamt": Numberlong (0),
"Mername": " Cloud Hi-Tech Services Limited ",
" AgentName ":" Baishan Institution "
}

Use Mongoexport to export the table:

$ mongoexport--host localhost--port 27017--username quicktest--password quicktest--collection trans.sp--db --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 Trans.sp.json size is 2.6 G, a total of 2.8 million records, the export time is 6 minutes. Specific description of the parameters:
localhost after--host: To export the database Ip--port 27017: The quicktest after the instance node port number to be exported--username: Database user name--password QuickTest: TRANS.SP After the database user password--collection: quicktest after the table name to be exported--db:--out after the database name of the table to be exported Trans.sp.json: The file path to export ( Default view current folder)

2. Import of single table data

There is also a debit, MongoDB provided with the Mongoexport command relative to the command mongoimport. The following is an import command:
$ mongoimport--host localhost--port 27019--username quicktest--password quicktest--collection trans.sp--db --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 the parameters:

--After the host localhost: the database to be imported Ip--port 27019: After the instance node port number to be imported--username QuickTest: After the database user name--password QuickTest: TRANS.SP After the database user password--collection: quicktest after the table name to import--db: The database name--file after the table to be imported Trans.sp.json: The source file path to import, which defaults to the current folder, is as simple as that.
But there are some places to keep in mind:

Mongoimport The new database does not have the TRANS.SP table to import successfully before the import is performed; the Mongoexport and mongoimport are required to enter a different sequence of parameters; for the replica set, Mongoexport Data export can be either a primary node in a replica set, or a secondary node, and for a replica set, the Mongoimport data import must be a primary node; For export of large amounts of data, there is no production problem with mongoexport operation. For imports of large amounts of data (more than MB), it is easy to drag the primary node down with the Mongoimport operation because it causes the primary node to crash due to full synchronization, so avoid importing large amounts of data in the production environment. The right thing to do is to create a single point, import, and then create a replica set. The MongoDB version number used in the example in this article is 3.2.1.

About MongoDB single table data export and recovery example explain knowledge to everyone here, hope to help!

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.