Few Commands about How to Export/Import MongoDB Collections

2014/05/311 min read
bookmark this

Table of Contents

  1. Introduction
  2. Export Collection from Mongo Database
  3. Import Collection to Mongo Database
  4. Import Collection to Mongo Database (Remote)
  5. Reference
  6. Conclusion

Introduction

The following examples show how to export and import collections to and from your localhost's MongoDB database.

Export Collection from Mongo Database

Mongodb/bin>mongoexport --db {your database} --collection {your collection} --out {your file name}.json

Import Collection to Mongo Database

Mongodb/bin>mongoimport --db {your database} --collection {your collection} --file {your file name}.json

Import Collection to Mongo Database (Remote)

Note: the --collection flag needs to come before --db.

Mongodb/bin>mongoimport --host {host server name} --port {server port number} --username {db username} --password {password} --collection {collection name} --db {database name} --file {your file name}.json

Reference

For detailed information, reference the following MongoDB documentation:

  • mongoexport

  • mongoimport

Conclusion

These simple mongoexport and mongoimport commands allow you to quickly export and import MongoDB collections, whether working locally or with a remote database. Always ensure you specify the correct database name, collection name, and file path when running these commands.