1. Create a Backup Folder in the Elixir Ambience Directory
To start, you’ll need a dedicated folder where all the exported data will be stored:
- Navigate to the Elixir Ambience directory on your local machine.
- Create a new folder named backup (or any preferred name) to store the exported data files.
- Enter the backup folder and copy its file path (you’ll need it later to reference the location when running the mongoexport and mongoimport commands).
2. Prepare the Environment for Exporting Data
You’ll need to run the mongoexport and mongoimport commands in a separate command prompt window.
-
Open a new command prompt (cmd) window.
-
Navigate to the backup folder you just created by entering this command:
cd C:\Elixir\Ambience-2024.0.3\backup
This will ensure you’re in the correct folder where your export files will be saved.
-
Set the path to MongoDB tools: The MongoDB tools (such as mongoexport and mongoimport) are not always included in your system’s default PATH, so you must set the path to the directory containing the MongoDB tools manually.
set path="C:\Program Files\MongoDB\Tools\100\bin"
Why Set the Path?
- Purpose: The set path command tells your command prompt where to find MongoDB tools (like mongoexport and mongoimport) so you can use them without needing to specify their full location every time.
- Where It Points: The path “C:\Program Files\MongoDB\Tools\100\bin” should be where MongoDB Tools are installed. If they’re installed in a different directory, adjust this path accordingly.
- Temporary: This change is temporary and only affects the current command prompt session. If you open a new command prompt window, you’ll need to set the path again.
3. Export Users, Roles, & Privileges from the Current Database
Now that you’re in the right folder and the MongoDB tools are accessible, it’s time to export the collections. Use the following commands to export each collection (Users, Roles, and Privileges) into individual JSON files:
mongoexport --uri=”<your CURRENT connection string>/<database>" --collection=Users --out=outputUsers.json
mongoexport --uri="<your CURRENT connection string>/<database>" --collection=Roles --out=outputRoles.json
mongoexport --uri="<your CURRENT connection string>/<database>" --collection=Privileges --out=outputPrivileges.json
Guide:
- –uri: This specifies the MongoDB connection string to your source database.
- –collection: This specifies the collection you want to export (e.g., Users, Roles, Privileges).
- –out: This tells mongoexport where to save the exported data file and the name of the file (e.g., outputUsers.json).
After running each mongoexport command, you should see a confirmation message similar to the following:
Note: Do not close this command prompt after executing these commands, as you’ll need it later for importing the data.
4. Drop the Privileges Collection in the New Database
Before importing the data into the new database, you need to ensure that the Privileges collection is not already present (to avoid any conflicts). Here’s how to drop the collection in the new database:
-
Open a new command prompt window (separate from the one you used for exporting).
-
Connect to the new MongoDB instance where you want to import the data:
mongosh "mongodb+srv://<your username>:<your user password>@clustertest.ard3t.mongodb.net/"
-
Switch to the ambience database:
use ambience
-
Drop the User and Privileges collection to ensure that the import doesn’t conflict with existing data:
db.Privileges.drop()
db.Users.drop()
Why Drop the Collection?
- Avoid Duplication: If the collection already exists in the new database, it could lead to duplicate or conflicting data when importing. Dropping it ensures you’re working with a clean slate.
- Prevents Errors: Some import operations might fail if the collection already exists, so removing it beforehand avoids potential errors.
5. Import Users, Roles, & Privileges into the New Database
Now that you’ve exported the necessary collections and dropped any conflicting collections, it’s time to import the data into the new database.
-
Go back to the original command prompt where you navigated to the backup folder and execute the following commands to import the data:
mongoimport --uri="<your NEW connection string>/<database>" --collection=Users --file=outputUsers.json
mongoimport --uri="<your NEW connection string>/<database>" --collection=Roles --file=outputRoles.json
mongoimport --uri="<your NEW connection string>/<database>" --collection=Privileges --file=outputPrivileges.json
Guide:
- –uri: The connection string to the new MongoDB instance.
- –collection: Specifies the collection to import data into (Users, Roles, or Privileges).
- –file: Specifies the path to the JSON file that contains the data to be imported (e.g., outputUsers.json).
After running each mongoimport command, you should see a confirmation message similar to the following: