top of page

Using MongoDB atlas and IP Whitelist



MongoDB is a renowned and open-source document-oriented database that can hold a large amount of data while allowing you to work with it very efficiently in the IT industry. It is classified as a NoSQL (Not simply SQL) database since data in MongoDB is not stored and retrieved in tables.


MongoDB is a database that was created and operated by MongoDB.Inc under the SSPL (Server Side Public Licence) was first released in February 2009. It also provides official driver support for all leading programming languages such as C, C++, C#, etc.Net, Go, Java, Node.js, Perl, PHP, Python, Motor, Ruby, Scala, Swift, and Mongoid are all examples of programming languages.


As a result, you can write an application in any of these languages.


How does it work?


Now we'll look at what goes on behind the scenes. As we all know, MongoDB is a database server that stores data in databases. In other words, the MongoDB environment provides a server on which you may launch MongoDB and build several databases.


The data is kept in collections and documents due to the NoSQL database. As a result, the database, collection, and documents are linked as follows:


Collections exist in the MongoDB database in the same way that tables exist in the MYSQL database. You are permitted to build several databases and collections.


We now have papers within the collection. These documents hold the data we want to store in the MongoDB database, and a single collection can contain several documents. The fact that you are schema-less implies that one document does not have to be equivalent to another.


The fields are used to build the documents. Fields in documents are key-value pairs, similar to columns in a relational database. The fields' values can be of any BSON data type, such as double, string, boolean, etc.


The data in MongoDB is saved in the form of BSON documents. BSON is an abbreviation for the Binary representation of JSON documents. In other words, the MongoDB server turns JSON data in the backend into a binary version known as BSON, which is stored and searched more effectively.


It is possible to store nested data in MongoDB documents. This data nesting lets you construct complicated relationships between data and keep them in the same document, which makes dealing with and retrieving data much more efficient than SQL.


You must use complex SQL joins to retrieve data from tables 1 and 2. The BSON document can be up to 16MB in size.


Note: You can run numerous databases on the MongoDB server.


Example: If we have a database called CipherSchools, for example. This database contains two collections, each of which includes two documents. And we keep our data in the form of fields in these papers.


Set up a MongoDB Atlas Account


MongoDB Atlas is a database-as-a-service platform configuring and hosting the database for you. Your only task will then be to populate your database with essential data.


● Firstly, Sign up for a new MongoDB Atlas account here.

● Fill in your details in the registration form and click Sign up.


Setting Up a New Cluster in MongoDB


Step 1: Fill in your organization's and project's names on the next screen, choose JavaScript as your desired programming language, and click the green Continue button.


Step 2: After you've created and verified your account, respond to the onboarding questions and click the green Finish button.


Step 3: Click the Create button under the Shared cluster type on the "Deploy a cloud database" page. This should be the only free choice:


Step 4: Leave the Cloud Provider & Region dropdown at its default settings. Your worth there will be determined by the region you live in.


Step 5: Leave M0 Sandbox (Shared RAM, 512 MB Storage) as the default in the Cluster Tier selection.

Step 6: Name your cluster or leave it as the default, Cluster0, in the Cluster Name selection.


Step 7: In this step, at the bottom of the screen, click on the green-colored Create Cluster button.


Step 8: The message "M0 Cluster Provisioning... should now appear." This will take about 3-5 minutes." Before proceeding, wait until the cluster has been built.


Make a New User in the Database


Step 1: Click on Database Access on the left side of the screen under SECURITY.


Step 2: Click the Add New Database User button in green.


Step 3: Make sure Password is selected under Authentication Method, then provide a username and Password for your user.


Step 4: If there is a Database User Privileges option, leave it as the default - it should be Read and Write to any database. If the default is blank, you may need to pick this manually. Choose "Read and write to any database" from the "Add Built-In Role" menu.


Step 5: Click the Add User button to add a new user.


Allow all IP Addresses to Connect.


Step 1: Click on Network Access on the left side of the screen under SECURITY.


Step 2: Click the Add IP Address button in green.


Step 3: Click the ALLOW ACCESS FROM ANYWHERE button on the modal. 0.0.0.0/0 should be displayed in the Access List Entry field.


Step 4: Then press the green Confirm button.


Connecting to Your Cluster


Step 1: Click on Database on the left side of the screen, under DEPLOYMENT.


Step 2: Connect your cluster by clicking the Connect button:


Step 3: Click Connect your application in the popup modal.


Now, the URI string you'll use to connect to your database should look something like this: mongodb+srv://<username>:< Password>@<cluster-name>.prx1c.mongodb.net/<db-name>?retryWrites=true&w=majority.


Step 4: Then, click on the copy button to copy your URI to your clipboard.


The URI you copied already has the username> and cluster-name> columns filled in for you.

Replace the?retryWrites=true&w=majority> field with the one you generated in the previous step, and make sure to include the name of your database before the query string (?retryWrites=true&w=majority).


You can name your database whatever you want, but a unique name for your project is a good idea. For example, if you're working on the "MongoDB and Mongoose" challenges, you could change db-name> to fcc-mongodb-and-mongoose or something similar.


Connecting an Existing Database


If you've already constructed a cluster and a database and want to link them to a new application, proceed as follows:


Step 1: Click on Database on the left side of the screen, under DEPLOYMENT.


Step 2: Locate your cluster and click the Browse Collections option to see a list of existing databases and collections.


Step 3: Replace db-name> in the URI string above with the database name you want to connect to.


That's all there is to it - you now have the URI to add to your application and connect to your database. Keep this URI somewhere safe in case you need it later.


Unlike many other "MongoDB as a service" cloud providers, MongoDB Atlas only accepts client connections to the cluster from entries in the group's whitelist.


Each item is either a single IP address, a CIDR-notated range of addresses, or a Security Group associated with the peer VPC if utilizing VPC Peering Connection. The whitelist, which can have up to 20 entries, applies to all clusters in the group.


What exactly is an IP address?


Firstly, let us understand the simple definition of IP Address. It is an IP address is a one-of-a-kind identifier for a device that is on the Internet or a local network. IP is an abbreviation for "Internet Protocol," a collection of rules governing the data format transferred over the Internet or a local network.


IP addresses are the identifiers that allow information to be exchanged between network devices: they contain location information and make equipment available for communication. The Internet requires a method to distinguish between different computers, routers, and webpages. IP addresses enable this and are critical to the Internet's operation.


IP addresses are not generated at random. The Internet Assigned Numbers Authority (IANA), a component of the Internet Corporation for Assigned Names and Numbers (ICANN), develops and assigns them mathematically. ICANN is a non-profit organization that was founded in the United States in 1998 to help preserve the security and usability of the Internet.


Whenever someone registers a domain name on the Internet, they go via a domain name registrar, who pays a nominal fee to ICANN to do so.

What exactly is the MongoDB Atlas whitelist?


The whitelist creates command generates a whitelist for the Atlas project supplied. One or more trustworthy IP addresses, AWS security group IDs, or entries in Classless Inter-Domain Routing (CIDR) notation can be included in the whitelist. Each project can have one whitelist.


Syntax:

mongocli atlas whitelist create <whitelist-entry>
     --comment "<description-of-value>"
     --deleteAfter <date-of-deletion>
     [ --output|-o <output-format> ]
     [ --profile|-P <name-of-profile> ]
     --projectId <id-of-project>
     [ --type cidrBlock|ipAddress|awsSecurityGroup ]


Note: The command does not overwrite existing whitelist entries. Instead, new entries are added to the list of entries. You may also use the Atlas UI or API to build a whitelist.


Example 1:


The following example command uses the default profile to generate a whitelist item for an IP address.


mongocli atlas whitelist create 192.0.2.15 --type ipAddress --projectId 5e2211c17a3e5a48f5497de3 --comment "IP address for app server 2" --output json


Output:

[
{
    "comment": "IP address for app server 7",
    "groupId": "5e2211c17a3e5a48f5497de3",
    "cidrBlock": "192.0.2.15/32",
    "ipAddress": "192.0.2.15"
 
},
 
{
    "comment": "IP address for app server 1",
    "groupId": "5e2211c17a3e5a48f5497de3",
    "cidrBlock": "10.201.30.114/32",
    "ipAddress": "10.201.30.114"
 
}
]


Example 2:


Using the default profile, the following command adds a whitelist item in CIDR notation. mongocli atlas whitelist create 73.231.201.205/24 --type cidrBlock --projectId 5e2211c17a3e5a48f5497de3 --output json --comment "CIDR block for servers C - F"


Output:

[
 
{
    "comment": "CIDR block for servers A - D",
    "groupId": "5e2211c17a3e5a48f5497de3",
    "cidrBlock": "73.231.201.205/24",
    "ipAddress": "73.231.201.205"
 
},
 
{
    "comment": "IP address for app server 1",
    "groupId": "5e2211c17a3e5a48f5497de3",
    "cidrBlock": "192.0.2.15/32",
    "ipAddress": "192.0.2.15"
 
},
 
{
    "comment": "IP address for app server 1",
    "groupId": "5e2211c17a3e5a48f5497de3",
    "cidrBlock": "10.201.30.114/32",
    "ipAddress": "10.201.30.114"
 
}
]


Adding an IP address to the MongoDB Atlas Whitelist


Step 1: Select the Security tab from the Clusters view, click IP Whitelist, and then Add IP Address.


Step 2: Enter the IP address or security group you want to use.


Step 3: Save and close the window.


Note: If you frequently test or develop your software on your computer, you should whitelist your home or workplace IP address.


You can also add many IP addresses at once by separating them by using comma or an entire range using CIDR notation (22.22.22.22/32).




MongoDB Features –


MongoDB offers a nice feature called schema-less databases. A schema-less database allows multiple types of documents to be stored in a single collection. In simple words, in the MongoDB database, a single collection can hold numerous documents, each with a different amount of fields, content, and size. One document doesn't need to be similar to another, as in relational databases. MongoDB's interesting feature gives databases a lot of flexibility.


Documents are Oriented: In MongoDB, all data is kept in documents rather than tables, as in RDBMS. In these documents, data is kept in fields like key-value pairs rather than rows and columns, making the data far more flexible compared to RDBMS. And each document has a distinct object id.


Indexing: Every field in the documents in the MongoDB database is indexed with the help of primary and secondary indices, making it easier and taking less time to get or search data from the data pool. If the data is not indexed, the database can search each document individually with the supplied query, which takes a long time and could be more efficient.


Scalability: MongoDB offers horizontal scalability via sharding. Sharding is the process of distributing data across numerous servers. A vast quantity of data is partitioned into several data chunks using the shard key, and these data pieces are distributed evenly across shards that exist across many physical servers. It will also add additional machines to an already active database.


Replication: With the aid of repetition, MongoDB enables high availability and redundancy by creating several copies of the data and sending these copies to a separate server so that if one server fails, the data may be accessed from another.


Aggregation: It allows you to conduct actions on aggregated data to obtain a single or computed output. It is analogous to the SQL GROUPBY clause. It offers three types of aggregations: aggregation pipelines, map-reduce functions, and single-purpose aggregation methods.

High Performance: Due to capabilities such as scalability, indexing, and replication, MongoDB has a very high performance and data persistence when compared to other databases.


Conclusion


Finally, MongoDB Atlas and IP Whitelisting offer substantial advantages to organizations seeking a dependable and secure database management solution.


MongoDB Atlas is a popular and fully managed cloud database service, has several benefits. It removes the need for businesses to manage their database infrastructure, decreasing operational complexities and expenses.


Users can scale their databases with Atlas to manage increasing workloads, maintaining optimal performance and responsiveness. The service also provides data replication and automated backups, ensuring data durability and availability.

In contrast, IP Whitelisting gives extra security to MongoDB Atlas deployments. Organizations can restrict database access to specific IP addresses or IP ranges by enabling IP Whitelisting, thereby prohibiting unauthorized access attempts. This capability is critical in protecting sensitive data from potential security issues like unauthorized access or harmful assaults.


MongoDB Atlas and IP Whitelisting create a robust and secure database management environment. Atlas provides a vital infrastructure with extensive monitoring and management features, and IP Whitelisting guarantees that only trustworthy sources can access the database.


This dual strategy decreases the risk of unauthorized access while also assisting organizations in meeting industry security standards and laws.


Furthermore, MongoDB Atlas and IP Whitelisting are highly scalable and adaptable solutions that meet the changing requirements of enterprises of all sizes. Whether a company is a small startup or a huge corporation, it can quickly alter its setups and scale its databases to meet growing demands without disturbing ongoing operations.


Thus, combining MongoDB Atlas and IP Whitelisting provides organizations with a dependable, scalable, and secure database management solution. Businesses may focus on their core activities while knowing that their data is safe and only available to authorized personnel by employing these technologies.


Interesting F.A.Qs

Which companies mostly use MongoDB?

Numerous firms, like Facebook, Nokia, eBay, Adobe, and Google, use MongoDB to store massive amounts of data.


How is MongoDB used in the healthcare industry?

MongoDB in healthcare is a database that is ready to consolidate all of your patient data, medical records, claims data, policies, and treatment information. Healthcare practitioners may access accurate and up-to-date patient information in real-time thanks to MongoDB's unrivaled document flexibility, scalability, performance, and security capabilities.


Stay tuned to CipherSchools for more interesting tech articles.

16 views

Recent Posts

See All
bottom of page