How to clone a collection in MongoDB (but with different name)

Updated: February 3, 2024 By: Guest Contributor Post a comment

Introduction

Interacting with NoSQL databases involves a diverse set of operations, including cloning collections which is particularly useful during tasks such as backing up data, setting up test environments, or migrating data within the same database. MongoDB, as a leading NoSQL database, provides a simplicitic yet powerful architecture when cloning collections. In this tutorial, we will take an in-depth look at how to clone a collection in MongoDB with a different name. We will move step-by-step from basic cloning to more advanced techniques, including code examples and expected outputs.

Before proceeding, ensure you have MongoDB installed and running on your system. You should also have basic familiarity with running commands in the mongo shell or using a MongoDB GUI tool like MongoDB Compass. Furthermore, you should have at least read permissions to the source collection and write permissions to the destination database.

Basic Collection Cloning

Cloning a collection in MongoDB with a different name is not an inbuilt single command operation. However, it can be achieved relatively simply using a combination of MongoDB commands. The most straightforward method is to copy all documents from the source collection to a new collection. This process can be achieved with the following steps using MongoDB shell or drivers in various programming languages:

db.sourceCollection.find().forEach(function(doc){
   db.getSiblingDB('destinationDB').newCollection.insertOne(doc);
});

This script will iterate over all documents in ‘sourceCollection’ from the current database and insert them into ‘newCollection’ within ‘destinationDB’. Note that ‘newCollection’ will be created automatically if it does not already exist.

Using Aggregation Pipeline

The aggregation framework in MongoDB provides a more efficient way to clone a collection. It enables data processing in stages, which can lead to improved performance, especially for large collections. The $out stage is particularly helpful as it allows the results of the aggregation to be written directly to a specified collection. This method is more succinct and is beneficial when data transformation is also required:

db.sourceCollection.aggregate([
    { $match: {} }, // This match is optional, it includes all documents
    { $out: 'newCollection' }
]);

The code above creates a new collection (or overrides an existing collection) named ‘newCollection’ that includes all documents from ‘sourceCollection’. It’s worth mentioning that the $out stage replaces the entire content of the destination collection.

Cloning with Filtering

There may be scenarios where you only want to clone a subset of the data from the source collection. You can achieve this by including a $match stage in the aggregation pipeline with your desired query criteria. For instance:

db.sourceCollection.aggregate([
    { $match: { status: 'active' } },
    { $out: 'activeOnlyCollection' }
]);

This will clone only documents that have a ‘status’ field with the value ‘active’ from ‘sourceCollection’ to ‘activeOnlyCollection’.

Advanced Cloning with Data Transformation

When cloning a collection, you might also want to transform the data as it’s being copied. The MongoDB aggregation pipeline shines here by allowing you to include other stages such as $project, $addFields, or even $group:

db.sourceCollection.aggregate([
    { $match: { isActive: true }},
    { $project: { name: 1, email: 1, isActive: 1, _id: 0 }},
    { $addFields: { cloneDate: new Date() }},
    { $out: 'transformedCollection' }
]);

The code snippet above demonstrates a pipeline that filters active users, projects specific fields (excluding the _id), adds a ‘cloneDate’ field to each document, and writes the results to a new collection named ‘transformedCollection’.

Considerations for Sharded Collections

If you’re working with sharded collections, you should be aware that the $out stage will not automatically shard the new collection for you. After cloning the data, you will need to shard ‘newCollection’ manually. Always check your sharding strategy before performing large write operations in sharded environments to avoid performance bottlenecks.

Automating Clone Operations

In some cases, you might want to automate the cloning process. For instance, creating a backup collection on a scheduled basis. This can be done using a CRON job or a similar task scheduler which runs a script containing the MongoDB commands used in previous examples. Such operations should be carefully managed to ensure they do not affect the database’s performance during peak hours.

Below is an example that demonstrates how to automate the process of cloning a MongoDB collection to create a backup on a scheduled basis using a CRON job on a Linux system. This script uses MongoDB commands to copy data from an existing collection to a backup collection. The operation is scheduled to run during off-peak hours to minimize impact on database performance.

Bash script:

#!/bin/bash

# MongoDB connection details
MONGO_HOST="localhost"
MONGO_DB="yourDatabase"
MONGO_USER="yourUser"
MONGO_PASS="yourPassword"

# Source and destination collection names
SRC_COLLECTION="sourceCollection"
DEST_COLLECTION="backupCollection_$(date +%Y%m%d%H%M)"

# MongoDB command to clone the collection
MONGO_CMD="mongo --host $MONGO_HOST -u $MONGO_USER -p $MONGO_PASS $MONGO_DB --eval 'db.$SRC_COLLECTION.aggregate([ { \$match: {} }, { \$out: \"$DEST_COLLECTION\" } ]);'"

# Execute the MongoDB command
eval $MONGO_CMD

# Log completion
echo "Backup of $SRC_COLLECTION completed at $(date)" >> /var/log/mongodb_backup.log

Steps to Automate with CRON:

  1. Create the Script: Save the above script to a file, e.g., /path/to/mongodb_backup.sh.
  2. Make the Script Executable: chmod +x /path/to/mongodb_backup.sh
  3. Open the CRON edit screen for the current user: crontab -e
  4. Schedule the script to run at a specific time. For example, to run daily at 2 AM: 0 2 * * * /path/to/mongodb_backup.sh
  5. Save and Exit: CRON will now run this script at the scheduled time.

Important Notes:

  • Customization: Replace yourDatabase, yourUser, yourPassword, and collection names with actual values.
  • Security: Ensure your MongoDB user has the necessary permissions to read from the source and write to the destination collections.
  • Performance Impact: Schedule backups during off-peak hours to minimize impact.
  • Logging: The script appends a log entry to /var/log/mongodb_backup.log after each execution. Ensure the executing user has write permissions to this file or adjust the log path as needed.
  • MongoDB Version: This script assumes MongoDB’s mongo shell utility is compatible with your MongoDB server version. Adjust commands as needed for compatibility.

Error Handling

While cloning collections, several errors might occur, such as write conflicts, permission issues, or disk space errors. Robust error handling and proper monitoring are critical while performing database operations programmatically. It is recommended to use driver-specific methods for error handling and to log appropriately for any anomalies encountered during cloning operations.

Conclusion

Cloning a MongoDB collection with a different name requires a mindful strategy involving the use of find() methods, aggregation frameworks, or a combination of MongoDB commands. The approach you select will depend on the size of your data, your need for transformation, and your performance considerations. With the techniques outlined in this tutorial, you should be equipped to clone and manipulate collections in MongoDB to meet various application requirements.