Connect to MongoDB instance using AWS Cloud9

Migration Module (2.2.1) - Migrate from MongoDB to Amazon DocumentDB using Database Migration Service (DMS)
  1. Go to the AWS Cloud 9 Console

  2. Click on Open the IDE: ide You are now in the AWS Cloud9 environment.

  3. Environment variables containing connection info about the source Mongo EC2 is are already set. Following commands creates the Mongo connection string and also prints the values.

     echo $mongoUser
     echo $mongoPass
     echo $mongoIp
  4. Log in to your MongoDB database:

    mongo mongodb://$mongoUser:$mongoPass@$mongoIp/labdb

    mongoshell After running the above command, you are connected to a local MongoDB instance running on an EC2 instance. This is similar to how customers run their MongoDB workloads in AWS.

  5. Our connection string took us to the “labdb” database. To see the collections within that database, use the following command:

    show collections


  6. You should see two collections, ”customer“ and ”locations“. You can view the details of these collections, which includes a count of the total documents and number, name, and size of indexes, by running the following commands:





  7. Additionally, we can view a sample document in each collection by running the following commands:



  8. Create the following indexes. To improve the performance of our queries, we can add indexes on our collections. Later in this lab we’ll walk through the steps to migrate these indexes to Amazon DocumentDB.

    Execute the following commands to create indexes on your collections:

    db.customers.createIndex( { touchproduct: 1 } )
    db.locations.createIndex( { action : 1 , loc : "2dsphere" } )


  9. The first command creates a simple, single field index on the touchproduct field of the customers collection. We can utilize this new index by running the following query:

    db.customers.find( { touchproduct: { $gt: 100, $lt:150 } } )


  10. The second index created was a geospatial index against the “loc” field on the locations collection. We can utilize this new index by running the following query:

db.locations.find( { loc :{ $geoWithin :{ $geometry :{ type :"Polygon" ,coordinates : [ [
[ -22 , -47 ] ,
[ -22 , -46 ] ,
[ -24 , -46 ] ,
[ -24 , -47 ] ,
[ -22 , -47 ]]
]} } } } )


  1. Now that you are familiar with the data in our source MongoDB instance, we can exit the mongo shell connection and proceed to the next step, connecting to our Amazon DocumentDB cluster: