Professional Documents
Culture Documents
Tintu Alphonsa Thomas Assistant Professor, CSE Amal Jyothi College of Engineering, Kanjirappally
Tintu Alphonsa Thomas Assistant Professor, CSE Amal Jyothi College of Engineering, Kanjirappally
Integer Integer is used to store the numeric value. It can be 32 bit or 64 bit depending on the server you are using
Boolean This datatype is used to store boolean values. It just shows YES/NO values
Double Double datatype stores floating point values.
Arrays This datatype is used to store a list or multiple values into a single key.
Date This datatype stores the current date or time in unix time format. It makes you possible to specify your own date time by
creating object of date and pass the value of date, month, year into it.
NoSQL Databases
NoSQL Database is used to refer a non-SQL or non relational
database.
It provides a mechanism for storage and retrieval of data other than
tabular relations model used in relational databases.
NoSQL database doesn't use tables for storing data.
It is generally used to store big data and real-time web applications.
MongoDB Advantages
• mongoDB is schema less.
• It is a document database in which one collection holds different
documents.
• There may be difference between number of fields, content and
size of the document from one to other.
• Structure of a single object is clear in MongoDB.
• There are no complex joins in MongoDB.
• It uses internal memory for storing working sets and this is the
reason of its fast access.
MongoDB Atlas provides an easy way to host and manage your data
in the cloud.
Creating an Atlas cluster, connecting to it, inserting data, and
querying data
https://www.mongodb.com/cloud/atlas/register
Get Started with Atlas
1. Click Sign up with Google.
2. Enter one of the following identifiers for your Google account:
Your Gmail or Google Apps email address
Phone number associated with your Google account
3. Click Next.
4. Enter the password for your Google account.
5. Click Next.
6. Review and select the checkbox to accept the Terms of Service and
the Privacy Policy.
7. Click Submit.
Create an Atlas Organization and Project
Create an Atlas organization and then create a project in this
organization.
You will deploy your first cluster in this project.
With your Atlas account, open the organization and its project, and
then proceed to Deploy a Free Tier Cluster.
https://docs.atlas.mongodb.com/tutorial/deploy-free-tier-cluster/
Deploy a Free Tier Cluster
Atlas Free Tier clusters provide a small-scale development
environment to host your data.
Free Tier clusters never expire, and provide access to a subset of
Atlas features and functionality.
Paid clusters provide full access to Atlas features, configuration
options, and operational capabilities.
You can deploy only one Free Tier cluster per Atlas project.
Procedure
Log into Atlas
Once you log in, open your organization and find your project.
Atlas prompts you to build your first cluster
Build a Cluster
Select Starter Clusters and click Create a Cluster.
Select your preferred Cloud Provider & Region
Select M0 Sandbox for cluster tier
Selecting M0 automatically locks the remaining configuration
options.
If you cannot select the M0 cluster tier, return to the previous step
and select a Cloud Provider & Region that supports M0 Free Tier
clusters.
Enter a name for your cluster in the Cluster Name field
You can enter any name for your cluster.
Click Create Cluster to deploy the cluster
Once you deploy your cluster, it can take up to 10 minutes for your
cluster to provision and become ready to use.
Add Your Connection IP Address to IP Access List
An IP is a unique numeric identifier for a device connecting to a
network.
In Atlas, you can only connect to a cluster from a trusted IP address.
Within Atlas, you can create a list of trusted IP addresses, referred to
as a IP access list, that can be used to connect to your cluster and
access your data.
You must add your IP address to the IP access list before you can
connect to your cluster. To add your IP address to the IP access list
Click Connect
Click Add Your Current IP Address.
Click Connect
In the Clusters view, click Connect for the cluster to which you
want to connect.
Set the new user's Username and Password
You'll use this username and password combination to grant a user
access to databases and collections in your cluster in Atlas project.
Click Create Database User
Connect to Your Atlas Cluster
In Atlas, you can connect to your cluster using the following
connection methods:
•Connect with the mongo shell to interact with your cluster using the
Javascript interface of the mongo Shell.
•Connect your application to your cluster using the Node.js driver, or
the PyMongo driver.
•Connect to your cluster using MongoDB Compass to explore, modify,
and visualize your data with Compass.
Connect to Your Cluster
Download and Install Utility
Download the installer from the MongoDB Community Downloads.
Add the mongo shell to your system path.
To run the mongo shell from your terminal, you will need to add the
shell to your system path.
Add the bin directory to your command path.
1.Select Control Panel.
2.Click System and Security, then System, then Advanced System
Settings.
3.Click Environment Variables.
4.Select Path and click Edit.
5.Add the bin directory where the installer placed your MongoDB
executables and click OK.
You should now be able to run the mongo shell using your terminal.
Test your mongo shell installation
To see if you have correctly added the mongo shell to your system
path, run the following command in your terminal:
mongo --version
Create Operations
Read operations
Update Operations
Delete Operations
1. Create Operations
Insert and View Data in Your Cluster Using the Shell
.
Switch to a new database called gettingStarted
In the mongo, run the following command:
use gettingStarted
> db.createCollection("people")
WriteResult({ "nInserted" : 1 })
Inserting Multiple Documents
.
There are two other ways for inserting documents other than insert()
db.people.insertOne({name:'name1’})
db.people.insertMany([{name:'name3'},{name:'name4'}])
db.people.find().pretty()
When you run this command, you should see the following output:
You may see a different value for ObjectId, because it is a system-generated
value.
db.people.find().pretty()
{
"_id" : ObjectId("60d5f084be3d27f0037e3028"),
"name" : {
"first" : "Alan",
"last" : "Turing"
},
"birth" : ISODate("1912-06-22T18:30:00Z"),
"death" : ISODate("1954-06-06T18:30:00Z"),
"contribs" : [
"Turing machine",
"Turing test",
"Turingery"
],
"views" : NumberLong(1250000)
}
{ "_id" : ObjectId("60d5fea0be3d27f0037e3029"), "name" : "name1" }
{ "_id" : ObjectId("60d60a7ebe3d27f0037e302a"), "name" : "name3" }
{ "_id" : ObjectId("60d60a7ebe3d27f0037e302b"), "name" : "name4" }
Specify the no. of results to be displayed
The limit() function in MongoDB is used to specify the maximum number of
results to be returned.
db.people.find().limit().pretty()
When you run this command, you should see the following output:
You may see a different value for ObjectId, because it is a system-generated
value.
{
"_id" : ObjectId("60d5f084be3d27f0037e3028"),
"name" : {
"first" : "Alan",
"last" : "Turing"
},
"birth" : ISODate("1912-06-22T18:30:00Z"),
"death" : ISODate("1954-06-06T18:30:00Z"),
"contribs" : [
"Turing machine",
"Turing test",
"Turingery"
],
"views" : NumberLong(1250000)
}
{ "_id" : ObjectId("60d5fea0be3d27f0037e3029"), "name" : "name1" }
Fetch documents with conditions
We can add conditions in the find()
db.people.find().sort({name:1}).pretty()
When you run this command, you should see the following output:
{ "_id" : ObjectId("60d5fea0be3d27f0037e3029"), "name" : "name1" }
{ "_id" : ObjectId("60d60a7ebe3d27f0037e302a"), "name" : "name3" }
{ "_id" : ObjectId("60d60a7ebe3d27f0037e302b"), "name" : "name4" }
{
"_id" : ObjectId("60d5f084be3d27f0037e3028"),
"name" : {
"first" : "Alan",
"last" : "Turing"
},
"birth" : ISODate("1912-06-22T18:30:00Z"),
"death" : ISODate("1954-06-06T18:30:00Z"),
"contribs" : [
"Turing machine",
"Turing test",
"Turingery"
],
"views" : NumberLong(1250000)
}
selects the documents where the value of the field is not equal to the specified value. T
When you run this command, you should see the following output:
OR Condition
$or operator performs a logical OR operation on an array of two or more and
selects the documents that satisfy at least one of the expressions.
When you run this command, you should see the following output:
count()
count() returns the number of documents in a collection.
When you run this command, you should see the following output: 1
selects the documents where the value of the field is not equal to the specified value. T
findOne()
findOne() returns the first document in a collection.
db.people.findOne()
When you run this command, you should see the following output:
count()
count() returns the number of documents in a collection.
When you run this command, you should see the following output: 1
3. Update Operations
replace an existing value with new value
modify specific fields of an existing document or documents or replace an existing
document entirely, depending on the update parameter.
remove() removes the document containing the property name and the
value associated property mentioned in the collection
db.people.remove({'name.last:'Turing'})
WriteResult({ "nRemoved" : 1 })
Conclusion
You've just set up an Atlas cluster to host your data, and used the
mongo shell to write and read data to and from your cluster.