MongoDB Finishing Notes Import and export

Source: Internet
Author: User

 First, import

1. Import JSON data

Let's remove the table user first to demonstrate the effect:

>true > show collections; System.indexes

Then import the data

[Email protected] bin]#./mongoimport-d My_mongodb-127.0.0.12 objects [[email protected] bin]#< /c7>

You can see that the table structure is created implicitly when importing data
2. Import CSV Data

Let's start with the tag user Delete to show the effect

>true  >

Then import the data

[[email protected] bin]#./mongoimport-d my_mongodb-c user--type csv--headerline--127.0.0.13 ob jects [[email protected] bin]#

Parameter description

Type indicates the file format to import

Headerline indicates that the first row is not imported

File indicates the path of the files to be imported
!!! Note: The CSV format is better, and the mainstream database supports exporting to CSV format, so this format is very beneficial for heterogeneous data migration.

Second, export

Suppose there's a user table in the library with 2 records, and we're going to export it

> Use my_mongodb  > db.user.find ();  " _id ": ObjectId (" 4f81a4a1779282ca68fd8a5a ")," UID ": 2," username ":" Jerry "," Age ":" _id ": ObjectId (" 4f844d 1847d25a9ce5f120c4 ")," UID ": 1," username ":" Tom "," Age ": 25

Similarly, there are two ways to export: JSON format and CSV format.

First, JSON format.

[Email protected] bin]#/mongoexport-d my_mongodb-c user-127.0.0.12 Records  [[email] Protected] bin]# cat User.dat  "_id": {"$oid": "4F81A4A1779282CA68FD8A5A"}, "UID": 2, "username": "Jerry", " Age ":" _id ": {" $oid ":" 4f844d1847d25a9ce5f120c4 "}," UID ": 1," username ":" Tom "," Age ": 25

Parameter description

D indicates which library to use

c indicates the table to be exported

o indicates the file name to export

Back to CSV format

[Email protected] bin]#/mongoexport-d my_mongodb-c user--csv-fuid,username,age-127.0.0.12 R Ecords  2, "Jerry", 1, "Tom",  [[email protected] bin]#

Parameter description:

CSV indicates that you want to export to CSV format

f indicates which columns to export

For more detailed usage, you can enter the command mongoexport-help to view

Note: In fact, the MONGODB export can be conditional, such as the following export is May 12 full-day data.

/usr/local/mongodb/bin/mongoexport-h 192.168.100.109--port 27017-d test-c testdb-q "{\" inittime\ ": {$gte: \" 2014-05-12 00:00:00\ ", $lte: \" 2014-05-12 23:59:59\ "}}"-O test.txt  

Description :

1, the collection structure must have the time domain, my is inittime

2, the-Q parameter is placed in the conditions you need to specify

3, note-Q the outermost is double quotation marks, and the inside of the double quotation marks plus the escape character "\"

4, and so on, if there is a gender in the collection structure, you can specify the gender export; age, you can specify age to export ...

5, the client can also export server data, format:--host 192.168.100.109--port 27017

MongoDB Finishing Notes Import and export

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.