MySQL data import to MONGO

Source: Internet
Author: User
Tags mongodb

background: title, you need to import a piece of data into the MONGO database to reduce the data source that the project relies on.

Solution:

Use the Mongoimport tool that comes with MONGO.

First create an empty collection in the test library: "Import_test", initially with the following import directives:

[Email protected] mongodb]#/bin/mongoimport--host192.168.104.178:17017-U adminuser-p112233--authenticationdatabase=admin-d test-c import_test--fields=userid,serial,create_time--type csv-file Csvtomongo.csv2018- .-11t18: -:07.075+0800Connected to:192.168.104.178:170172018- .-11t18: -:07.083+0800Imported theDocuments

Instruction Explanation:

    • --authenticationdatabase: The test cluster uses authentication and needs to be logged in the Admin library before it can be manipulated.
    • -D: The library where the collection resides
    • --fields: Field names in the collection

The results found that the MONGO field could not be created properly, as follows

Can only be imported in a different way, and later found to write the field name to the file, then import there will be no problem, as follows:

[email protected] mongodb]# cat Myfields.txt useridserialcreate_time

The three fields are saved in the myfields file, one per line, and then the import command is executed:

[Email protected] mongodb]#/bin/mongoimport--host192.168.104.178:17017-U adminuser-p112233--authenticationdatabase=admin-d test-c import_test--fieldfile myfields.txt--type csv--file Csvtomongo.csv2018- .-11t18: -:06.894+0800Connected to:192.168.104.178:170172018- .-11t18: -:06.903+0800Imported theDocuments

The results are as follows:

Finally, paste the test CSV data:

[email protected] mongodb]# cat csvtomongo.csv1504165,1, .- to- - Geneva: -: -1504585,1, .- to- - Geneva: -: -1556423,1, .- to- - Geneva: Geneva: $1557488,1, .- .- -  .:Wuyi: One1558460,1, .-Geneva- in  .: Wu: -1508659,1, .- to- -  to:Ten: the1500073,1, --Geneva- -  -: A: +1505064,1, .- -- on Geneva:Wuyi: the1507204,1, .- the- + Ten: -: the1506296,1, -- the- - Geneva: +: $1506765,1, .- -- on  -: -:Ten1506944,1, .- .- -  the: $: -1506162,1, -- A- the Ten: ,:Ten1506814,1, -- A- the  -: $: -1506014,1, .- to- -  .: -: -

Note: Finally, I finally know why the field name is not normal, and MONGO instructions, but the first instruction last parameter write error, file front only a bar "-", if write two bars, also have the correct result, hahaha

./bin/mongoimport--host 192.168.104.178:17017-u adminuser-p 112233--authenticationdatabase=admin-d test-c import_te St--fields=userid,serial,create_time--type csv --file csvtomongo.csv

I'm done.

MySQL data import to MONGO

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.