A2oz

How to Import a Collection in MongoDB Cloud?

Published in MongoDB 2 mins read

You can import a collection into your MongoDB Atlas cluster using the MongoDB Compass application or the MongoDB Shell.

Using MongoDB Compass

  1. Open MongoDB Compass: Launch the MongoDB Compass application on your computer.
  2. Connect to your cluster: Select the cluster you want to import the collection into.
  3. Navigate to the "Import" tab: In the left-hand navigation pane, click on the "Import" tab.
  4. Choose "Collection" as the import type: Select "Collection" from the available import options.
  5. Provide the source data: Specify the location of your source data, which can be a JSON file, CSV file, or another MongoDB collection.
  6. Configure import settings: Choose the collection name and database you want to import the data into. You can also specify options like creating the collection if it doesn't exist, overwriting existing data, and more.
  7. Start the import process: Click on the "Import" button to begin importing your collection.

Using MongoDB Shell

  1. Connect to your cluster: Open a MongoDB Shell window and connect to your Atlas cluster using the mongo command.
  2. Use the mongoimport command: Execute the mongoimport command with the necessary arguments to import the collection.
    • Source data: Specify the path to your source data file or the connection details of the source MongoDB collection.
    • Destination database and collection: Specify the database and collection name where you want to import the data.
    • Other options: You can customize the import process using various options such as --drop, --upsert, --jsonArray, etc.

Example:

mongoimport --db myDatabase --collection myCollection --file myData.json

This command imports data from the myData.json file into the myCollection collection within the myDatabase database.

Practical Insights:

  • Data format: Ensure your source data is in a valid format supported by MongoDB, like JSON, CSV, or another MongoDB collection.
  • Performance: For large datasets, consider using the mongoimport command for faster performance.
  • Error handling: Monitor the import process for any errors and resolve them accordingly.

Related Articles