Data export in MongoDB

Source: Internet
Author: User

1. there is a data export tool under the bin directory of the mongodb installation directory: Export export.exe. We can open a command window, switch to this directory, and execute export, you can see many parameter descriptions for using this tool.

Assume that the mongodb installation directory is D: \ program \ mongodb, you can use the export tool in the following ways:

>D:

>D: \ program \ mongodb \ bin

>Mongoexport

-- Help produce help message
-V [-- verbose] be more verbose (include multiple time
Verbosity e.g.-vvvvv)
-- Version print the program's version and exit

........

 

2. Common data export Methods

First, add a new localdb in MongoDB, create a collection user, and insert some objects.

>Use localdb

>Db. user. insert ({"name": "ming", "age": 2 })

>Db. user. insert ({"name": "lisi", "age": 20 })

 

Then, you can use the following method to export data in the user table. -D indicates the database from which data is exported;-c indicates the set from which data is exported;-o indicates the file to which the exported data is exported.

>Mongoexport-d localdb-c user-o user. dat

 

After the export is successful, open the user. dat file and export the data as follows:

{"_ Id": {"$ oid": "4eed9f9ca939118694cf05e4"}, "name": "ming", "age": 2}
{"_ Id": {"$ oid": "4ef1f3cf3bd18218e6bdfa31"}, "name": "lisi", "age": "20 "}

 

3. Export as a CVS file

If you want to save the data of a specified field in the export set to the CVS file, run the following command:

>Export export-d localdb-c user-f _ id, name -- csv-o user.csv

The exported user. cvs file is as follows:

{"_ Id": {"$ oid": "4eed9f9ca939118694cf05e4"}, "name": "ming "}
{"_ Id": {"$ oid": "4ef1f3cf3bd18218e6bdfa31"}, "name": "lisi "}

 

4. mongoexport parameter list

Options:
-- Help produce help message
-V [-- verbose] be more verbose (include multiple times for more verbosity e.g.-vvvvv)
-- Version print the program's version and exit
-H [-- host] arg mongo host to connect to (<set name>/s1, s2 for sets)
-- Port arg server port. Can also use -- host hostname: port
-- Ipv6 enable IPv6 support (disabled by default)
-U [-- username] arg username
-P [-- password] arg password
-- Dbpath arg directly access your D database files in the given
Path, instead of connecting to a mongod server-
Needs to lock the data directory, so cannot be used
If a primary D is currently accessing the same path
-- Directoryperdb if dbpath specified, each db is in a separate directory
-- Journal enable journaling
-D [-- db] arg database to use
-C [-- collection] arg collection to use (some commands)
-F [-- fields] arg comma separated list of field names e.g.-f name, age
-- FieldFile arg file with fields names-1 per line
-Q [-- query] arg query filter, as a JSON string
-- Csv export to csv instead of json
-O [-- out] arg output file; if not specified, stdout is used
-- JsonArray output to a json array rather than one object per line
-K [-- slaveOk] arg (= 1) use secondaries for export if available, default true

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.