Mongodb Export and Import
Mongodb Export and Import
1: import / export can operate on a local mongodb server or remotely.
Therefore, there are the following general options:
-h host host
-- port port port
-u username user name
-p passwd password
2: mongoexport exports files in json format
Q: which library, which table, which columns, which rows are exported?
-d library name
-c table name
-f field1,field2... Column name
-Q query condition
-o File name of the export
-- csv exports csv format (easy to exchange data with traditional databases)
Example:
[root@localhost mongodb] #. / bin/mongoexport-d test-c news-o test.json
Connected to: 127.0.0.1
Exported 3 records
[root@localhost mongodb] # ls
Bin dump GNU-AGPL-3.0 README test.json THIRD-PARTY-NOTICES
[root@localhost mongodb] # more test.json
{"_ id": {"$oid": "51fc59c9fecc28d8316cfc03"}, "title": "aaaa"}
{"_ id": {"$oid": "51fcaa3c5eed52c903a91837"}, "title": "today is sataday"}
{"_ id": {"$oid": "51fcaa445eed52c903a91838"}, "title": "ok now"}
Example 2: export only goods_id,goods_name columns
. / bin/mongoexport-d test-c goods-f goods_id,goods_name-o goods.json
Example 3: only export lines whose price is less than 1000 yuan
. / bin/mongoexport-d test-c goods-f goods_id,goods_name,shop_price-Q'{shop_price: {$lt:200}'- o goods.json
Note: the _ id column is always exported
Mongoimport Import
-d databases to be imported
-c the table to be imported (it will be created by itself if it does not exist)
-- type csv/json (default)
-- file backup file path
Example 1: import json
. / bin/mongoimport-d test-c goods-- file. / goodsall.json
Example 2: import csv
. / bin/mongoimport-d test-c goods-- type csv-f goods_id,goods_name-- file. / goodsall.csv
/ bin/mongoimport-d test-c goods-- type csv-- headline-f goods_id,goods_name-- file. / goodsall.csv (1. You need to specify a column name. 2.--headline skips the first line)
(the first line is useless)
Mongodump exports the data of binary bson structure and its index information
-d library name
-c table name
-f field1,field2... Column name
Example:
Mongodum-d test [- c table name] is exported to the dump directory under mongo by default
Rules:
1: the exported files are placed in a directory named database
2: each table exports 2 files, which are the data file of bson structure and the index information of json.
3: if you do not declare the table name, export all tables
Mongorestore imports binaries
Example:
. / bin/mongorestore-d test-- directoryperdb dump/test/ (backup directory at mongodump)
Binary backup, which can back up not only the data but also the index
The backup data is relatively small.