MongoDB in AWS
Overview
MongoDB is not supported by AWS directly, but NoSQL systems like DynamoDB and DocumentDB are. As a result, we will learn how to install MongoDB and operate MongoDB in AWS (Amazon EC2) as well as how to use a lambda code to execute shell commands on EC2.
MongoDB on AWS Cloud: Quick Start Reference Deployment
The MongoDB (version 2.6 or 3.0) cluster runs on Amazon EC2 and Amazon VPC and is started using an AWS CloudFormation template. The template can be used straight or copied, then customized as necessary. The following materials are produced by the template :
- Public and private subnets in a VPC.
- A NAT instance in the public subnet to enable outgoing Internet communication and SSH access to the cluster.
- A job for an IAM instance with specific rights.
- security teams.
- A completely customized MongoDB cluster operating in the private subnet, complete with replica sets, fragments, configuration servers, and customised EBS storage.
What is Amazon EC2 Instance?
Re-sizable elastic compute facilities in the cloud are offered by Amazon Elastic Compute Cloud (EC2), an online utility from Amazon. If your processing needs change, you can easily scale either increasing or decreasing the number of server nodes you use thanks to their resizability.
An on-demand EC2 are used to deploy the applications. An AWS user or client can install their apps by renting the virtual server by the hour. Depending on the sort of instance selected, a variety of hourly prices will apply to the instance. For the various business requirements of the customer, AWS offers a variety of instance kinds. As a result, you can hire an instance and use it for till whenever you like based on your own CPU and RAM needs. When the instance is no longer needed, you can end it and save money.
Installing MongoDB on AWS EC2 Instance
Step 1 : Create an EC2 server first.
Navigate to EC2 after logging into your AWS dashboard.
- Name: mongodb-instance
- AMI: Amazon Linux 2 AMI
- Instance type: t2.micro
- Architecture: 64-bit (x86)
- Key pair: create a new one or an existing one
- Security Groups: Allow traffic (SSH) from everywhere.
Step 2 : Install MongoDB in AWS
Open a terminal and navigate to the location where you kept the key combination once the EC2 instance status changes to Running.
To install MongoDB directly using yum, create a file called /etc/yum.repos.d/MongoDB-org-6.0.repo.
To load the most recent MongoDB, enter the following instruction.
To make sure MongoDB has been loaded properly, run the code below.
Now, mongoDB has been properly installed in the EC2 server.
Starting MongoDB on AWS EC2
Step 1: Run the following instructions to launch MongoDB in AWS :
Step 2: Use the following instruction to confirm that MongoDB has been properly started:
Now the MongoDB has been turned on.
CRUD Operations in Mongodb in AWS EC2 Instance
Let us now examine the CRUD (Create/Read/Update/Delete) actions that can be taken in MongoDB. Now enter the MongoDB shell and build a table and database structure. Give the following instruction :
To make a database, type use DATABASE_NAME. If no database is present, the command will make one; otherwise, it will restore the database that is there.
Now, make a user account by following the code given below.
Then we will try to insert some records in the newly created database.
Create :
Insert method is used to insert a document or documents into a collection. It takes a document or array of documents. Syntax of the insert function in MongoDB in AWS is as follows :
Example:
Read :
We use the find function to read documents from mongoDB. The syntaxthe of the find function in MongoDB in AWS is given below.
Description of parameters used in find method(all are document type):
- Query: It is optional. It is used to filter a few specific documents from the collection of all documents.
- projection: It is optional. Specifies the fields to return in the documents that match the query filter.
- options: It is optional. Specifies the fields to return in the documents that match the query filter.
Example:
To get the document that was entered, run the following command :
Please note that the record that was obtained has a property called _id with a special value for a document identifier called ObjectId. When calling the find() function with parameters to obtain a record based on a set of criteria, the record will be located using the characteristics that were provided.
Update :
The syntax of update function is as follows :
Description of parameters used in the update methQueryquery: It specifies the selection criteria for the update.
- update: It specifies modifications to apply.
- upsert: When true, update() either:
- Creates a new document if no documents match the query.
- Updates a single document that matches the query.
- multi: If set to true, updates multiple documents that meet the query criteria.
- collation: It allows users to specify language-specific rules for string comparison
- arrayFilters: An array of filter documents that determine which array elements to modify for an update operation on an array field.
- hint: A document or string that specifies the index to use to support the query predicate.
- let: This allows you to improve command readability by separating the variables from the query text.
Example:
Run the query below to modify certain field values in a group in MongoDB.
Delete :
We use the remove method to delete records or records. The syntax of remove method is as follows :
Description of the parameters used in the remove method.
- query: Specifies deletion criteria using query operators. To delete all documents in a collection, pass an empty document ({}).
- Justine: Optional. To limit the deletion to just one document, set it to true.
Example:
Now let's examine removing a record from a group. Run the following instruction to remove an item from a collection :
It should be noted that the item has been removed from the product collection after using the remove() function.
FAQs
Q: What is Amazon EC2 Instance?
A: Re-sizable elastic compute facilities in the cloud is offered by Amazon Elastic Compute Cloud (EC2), an online utility from Amazon. For the various business requirements of the customer, AWS offers a variety of instance kinds. As a result, you can hire an instance and use it for as long as you like based on your own CPU and RAM needs.
Q: MongoDB in AWS Cloud
A: MongoDB is not supported by AWS directly, but NoSQL systems like DynamoDB and DocumentDB are. As a result, we have to install MongoDB and operate it on Amazon EC2 as well as how to use a lambda code to execute shell commands on EC2.
Conclusion
- The MongoDB (version 2.6 or 3.0) cluster runs on Amazon EC2 and Amazon VPC and is started using an AWS CloudFormation framework.
- Re-sizable elastic compute facilities in the cloud are offered by `Amazon Elastic Compute Cloud (EC2), an online utility from Amazon.
- With an on-demand EC2 instance, an AWS user or client can install their apps by renting the virtual server by the hour.
- You can do all the CURD operations on the mongoDB on the E2C instance.