Professional Documents
Culture Documents
Unit-1
Unit-1
• In the early 1970, Flat File Systems are used. Data were stored in flat files and the
biggest problems with flat files are each company implement their own flat files
and there are no standards. It is very difficult to store data in the files, retrieve
data from files because there is no standard way to store data.
• Then the relational database was created by E.F. Codd and these databases
answered the question of having no standard way to store data. But later
relational database also gets a problem that it could not handle big data, due to
this problem there was a need of database which can handle every types of
problems then NoSQL database was developed.
Advantages of NoSQL
MongoDB
MongoDB Advantages
o Easy to use
o Light Weight
o Extremely faster than RDBMS
Data Description
Types
String String is the most commonly used datatype. It is used to store data. A string
must be UTF 8 valid in mongodb.
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.
Min/Max This datatype compare a value against the lowest and highest bson
Keys elements.
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.
Create a Database
From the Databases tab, click the Create Database button to bring up the Create Database
dialog.
• In the dialog, enter the name of the database to create and its first collection. Both
the database name and the collection name are required.
• If you want to create a capped collection, select the Capped Collection checkbox and
enter the maximum bytes.
• If you want to use custom collation on the newly created collection, select the Use
Custom Collation checkbox and select the desired collation settings.
Unit-1 Concepts of NoSQL: MongoDB
3. Click Create Database to create the database and its first collection.
Drop a Database
From the Databases tab, to delete a database, click on the trash can icon for that database. A
confirmation dialog appears.
Collections Screen
• The Collections screen lists the existing collections and views in the selected
database.
• Each list item includes the name and other general information for the collection or
view.
• To access the Collections screen for a database, from the Databases screen either:
✓ Click a Database Name in the main Databases view, or
✓ Click a database in the left navigation.
Unit-1 Concepts of NoSQL: MongoDB
Collection Information
The Collections screen displays the following information for each collection in the selected
database:
• Collection name
• Number of documents in the collection
• Average size of documents in the collection
• Total size of all documents in the collection
• Number of indexes on the collection
• Total size of all indexes on the collection
• Collation properties for the collection. Hover over a Collation banner to view the
properties for that collection.
Create a Collection
You can create new collections in an existing database.
✓ If you want to create a capped collection, select the Capped Collection checkbox and
enter the maximum bytes.
✓ If you want to use custom collation on the collection, select the Use Custom
Collation checkbox and select the desired collation settings.
Drop a Collection
1. Click the trash can icon to delete a collection.
From the Collections screen, click on the trash can for the collection to delete. A
confirmation dialog appears.
show dbs
use databasename
db
To delete database
Show collections
To create collections
db.createCollection(‘collectionName’)
To delete collections
db.<collectionname>.drop()
1.3 CRUD operations (Insert, update, delete, find, Query and Projection
operators)
Insert
The create or insert operations are used to insert or add new documents in the collection. If
a collection does not exist, then it will create a new collection in the database. You can
perform, create operations using the following methods provided by the MongoDB:
Method Description
Example: 1
Unit-1 Concepts of NoSQL: MongoDB
In this example, we are inserting details of a single student in the form of document in the
student collection using db.collection.insertOne() method.
Example 2:
In this example, we are inserting details of the multiple students in the form of documents
in the student collection using db.collection.insertMany() method.
Unit-1 Concepts of NoSQL: MongoDB
Update
The update operations are used to update or modify the existing document in the collection.
You can perform update operations using the following methods provided by the MongoDB:
Method Description
Example 1:
In this example, we are updating the age of Sumit in the student collection
using db.collection.updateOne() method. method.
Example 2:
In this example, we are updating the year of course in all the documents in the student
collection using db.collection.updateMany() method.
Unit-1 Concepts of NoSQL: MongoDB
Delete
The delete operation are used to delete or remove the documents from a collection. You
can perform delete operations using the following methods provided by the MongoDB:
Method Description
Example 1:
In this example, we are deleting a document from the student collection
using db.collection.deleteOne() method.
Find
The Read operations are used to retrieve documents from the collection, or in other words,
read operations are used to query a collection for a document. You can perform read
operation using the following method provided by the MongoDB:
Unit-1 Concepts of NoSQL: MongoDB
Method Description
Example 1:
In this example, we are retrieving the details of students from the student collection
using db.collection.find() method.
query document Optional. Specifies selection filter using query operators. To return
all documents in a collection, omit this parameter or pass an empty
document ({}).
projection document Optional. Specifies the fields to return in the documents that match
the query filter. To return all fields in the matching documents, omit
this parameter.
Unit-1 Concepts of NoSQL: MongoDB
1.3 Operators (Projection, update, limit (), sort ()) and Aggregation
Name Description
$gte Matches values that are greater than or equal to a specified value.
$lte Matches values that are less than or equal to a specified value.
$ne Matches all values that are not equal to a specified value.
Example
Logical
Name Description
$and Joins query clauses with a logical AND returns all documents that match the conditions of
both clauses.
$not Inverts the effect of a query expression and returns documents that do not match the
query expression.
$nor Joins query clauses with a logical NOR returns all documents that fail to match both
clauses.
Unit-1 Concepts of NoSQL: MongoDB
Name Description
$or Joins query clauses with a logical OR returns all documents that match the conditions of
either clause.
Array
Name Description
$all Matches arrays that contain all elements specified in the query.
$elemMatch Selects documents if element in the array field matches all the
specified $elemMatch conditions.
Projection Operators
Name Description
$ Projects the first element in an array that matches the query condition.
$slice Limits the number of elements projected from an array. Supports skip
and limit slices.
limit ()
Unit-1 Concepts of NoSQL: MongoDB
db.writers.find().pretty().limit(2)
Skip()
db.writers.find().pretty().skip(1)
sort ()
• For sorting your MongoDB documents, you need to make use of the sort() method.
• This method will accept a document that has a list of fields and the order for sorting.
• For indicating the sorting order, you have to set the value 1 or -1 with the specific
entity based on which the ordering will be set and displayed.
• One indicates organizing data in ascending order while -1 indicates organizing in
descending order.
• Db.test.find.sort({age:1})
Aggregation commands
• MongoDB's aggregate function will cluster out the records in the form of a collection
which can be then employed for providing operations like total number(sum), mean,
minimum and maximum, etc. from the aggregated group of data extracted.
• For performing such an aggregate function, the aggregate() method is used. The
syntax of this method looks something like this:
db.collection_name.aggregate(aggregate_operation)
Expression Description
$sum adds up the definite values of every documentUnit-1
of a Concepts
collection.
of NoSQL: MongoDB
$min finds and returns the minimum of all values from within a collection.
$max finds and returns the maximum of all values from within a collection.
Example:
• db.test.aggregate([
... { $group: {_id: "$age", count:{$sum:1}}}])