Mongo: mongoimport and mongoexport

05/04/2014 12:46 | DB, MongoDB, OS X

System: OS: OS X 10.9
Mongo: MongoDB shell version 2.4.9
Link: Mongo import and Mongo Export

MongoDB_1

Mongo import or export used for flexible manipulating with collections full databases or some parts of collections. With this instruments manipulating with data can be realised easily.

What specially you can do with that tools?

  • Import to existing db set of collections
  • Import to existing db one collection
  • Import to collection data from other collection
  • Export from db collections/part of collections
  • Export part of collection according to parameters. For example you can get from big collection only some part by day gap or by other search criteria.

Mongoexport

For exporting you need specify db name, collection and output file where result of exporting will be stored.

IMPORTANT! You can export to two file types. One of them CSV and other JSON. Personally I like JSON. In my experience I had less problems while future import with JSON files.

As a result you will get new folder export with JSON files.
Full example of mongoexport with all main parameters for exporting from server looks like this:

However main power of mongoexport in using query for exporting. Imagine that you have collection with 100 000 000 records and you need only some part for testing or only some particular record or only record for one month:

With this command you will get records that match with query only records where “userMail” field equals “user@mail.com”
Also one example of exporting with parameters SORT, LIMIT and SKIP:

Translation of this command looks like that: export from project_test_db database from users collection to export/users.json file also before exporting sort records in collection by fieldName field skip first 10 records and return maximum 100 records.

Mongoimport

When you already have exported JSON file with data what you need you can use mongoimport for inserting data to db.

In general mongoimport quite similar to mongoexport main difference in file parameter which specifies source JSON file for importing data.

© 2014 Artem Gryn personal blog