MongoDB Import and Export (7)

Source: Internet
Author: User
Tags mongodb server mongodump mongorestore

The import/export can operate on a local MONGODB server, or it can be remote.

Therefore, the following general options are available:

-H Host

--port Port Ports

-u username user name

-P passwd Password

Mongoexport exporting 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 criteria

-O Exported file name

--CSV Export CSV format (easy to exchange data with traditional database)

Cases:

[Email protected] mongodb]#/bin/mongoexport-d test-c news-o Test.json

Connected to:127.0.0.1

Exported 3 Records

[[email protected] mongodb]# ls

Bin dump gnu-agpl-3.0 README Test.json third-party-notices

[Email protected] 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: Export only rows with prices below $1000

./bin/mongoexport-d test-c goods-f goods_id,goods_name,shop_price-q ' {shop_price:{$lt: +} '-O Goods.json

Note: _id columns are always exported

Mongoimport Import

-D database to import

-c table to be imported (does not exist to be created by itself)

--type Csv/json (default)

--file backup File path

Example 1: Importing JSON

./bin/mongoimport-d Test-c Goods--file./goodsall.json

Example 2: Importing CSV

./bin/mongoimport-d test-c Goods--typecsv-f goods_id,goods_name--file./goodsall.csv

./bin/mongoimport-d test-c Goods--typecsv --headline -F goods_id,goods_name--file./goodsall.csv

Mongodump Exporting binary Bson structure data and its index information

-D Library Name

-C Table Name

-F Field1,field2 ... Column Name

Cases:

mongodump-d test [-C table name]--out export Path

Law:

1: The exported file is placed in a directory named database

2: Each table exports 2 files, respectively, Bson structure data files (such as: Goods.bson), JSON index information (such as: Goods.metadata.json)

3: If you do not declare a table name, export all tables

Example: mongodump-d test--out C:\Users\yuxl\Desktop\mongodb\mongod\mongodumptest

Mongorestore Importing binary files

Cases:

./bin/mongorestore-d Test--dir Path

Example: mongorestore-d test--dir C:\Users\yuxl\Desktop\mongodb\mongodumptest\test

Binary backup that not only backs up data, but also backs up indexes,

Backup data is relatively small.

MongoDB Import and Export (7)

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.