Copy MongoDB Collection to Another Database Easily
How to copy a MongoDB Collection to another Database?
Copying a collection from one MongoDB database to another is useful when you need to move or duplicate data for development, backup, or scaling purposes. This process can be done using MongoDB's aggregate() function with the $out stage if working within the same MongoDB instance, or by exporting and importing the collection across instances.
Syntax:
If copying within the same MongoDB instance, you can use the $out stage to direct the output of an aggregation into a new collection in another database:
db.<source_collection>.aggregate([ { $match: {} }, // Match all documents in the source collection { $out: "<destination_db>.<destination_collection>" } ]);
To copy a collection across different MongoDB instances, use mongoexport and mongoimport.
Example:
Method 1: Copy within the Same MongoDB Instance
Suppose you want to copy a collection named products from a database sourceDB to a database targetDB.
Code:
// Switch to the source database
use sourceDB;
// Perform the aggregation on the source collection
db.products.aggregate([
{ $match: {} }, // Match all documents
{ $out: "targetDB.copiedProducts" } // Output to targetDB's copiedProducts collection
]);
Explanation:
- use sourceDB: Switches to the source database.
- db.products.aggregate([...]): Starts an aggregation pipeline on the products collection.
- { $match: {} }: Matches all documents in products (no filtering).
- { $out: "targetDB.copiedProducts" }: Copies all documents to the copiedProducts collection in targetDB.
Example:
Method 2: Copy Across Different MongoDB Instances (Using mongoexport and mongoimport)
1. Export the Collection
From the command line, run the mongoexport command to export the products collection from sourceDB.
Code:
mongoexport --db=sourceDB --collection=products --out=products.json
Explanation:
- --db=sourceDB: Specifies the source database.
- --collection=products: Specifies the collection to export.
- --out=products.json: Exports the data to a JSON file.
2. Import the Collection
Then, use the mongoimport command to import the exported data into the target database.
Code:
mongoimport --db=targetDB --collection=copiedProducts --file=products.json
Command Explanation:
- --db=sourceDB: Specifies the source database.
- --collection=products: Specifies the collection to export.
- --out=products.json: Exports the data to a JSON file.
Explanation:
- Same Instance Using $out
- The $out stage in MongoDB’s aggregation pipeline directs the output to a specified collection. It replaces any existing data in the destination collection, making it a convenient tool for copying collections within the same MongoDB instance.
- The aggregation pipeline with $out effectively performs a deep copy, creating a new instance of each document in the specified target collection.
- Different Instances Using mongoexport and mongoimport
- mongoexport creates a JSON or CSV file of the source collection, which can then be imported into a different MongoDB instance using mongoimport.
- This is especially useful for moving data between different servers or environments (e.g., from development to production).
- Limitations
- The $out method only works within the same instance.
- mongoexport and mongoimport are more flexible but involve creating intermediate files, which may add some processing overhead..
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics