0% found this document useful (0 votes)
77 views

Lab - MongoDB P II PDF

The document provides an overview of MongoDB and how to perform basic operations like creating databases and collections, inserting documents, querying documents, and working with cursors. It explains that MongoDB is a non-SQL database used for high-volume data storage and that databases and collections can be created using the "use" and "insert()" commands respectively. It also demonstrates how to insert single documents and arrays of documents, query documents based on fields, and iterate through result sets using cursors.

Uploaded by

Hazrat Ullah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Lab - MongoDB P II PDF

The document provides an overview of MongoDB and how to perform basic operations like creating databases and collections, inserting documents, querying documents, and working with cursors. It explains that MongoDB is a non-SQL database used for high-volume data storage and that databases and collections can be created using the "use" and "insert()" commands respectively. It also demonstrates how to insert single documents and arrays of documents, query documents based on fields, and iterate through result sets using cursors.

Uploaded by

Hazrat Ullah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Air University, Islamabad

Department of Computer Science

Full Stack Web Development


MogoDB P-II

Overview
MongoDB is a document-oriented NoSQL database used for high volume data storage. In this free
course you will learn how Mongodb can be accessed and its important features like indexing, regular
expression, sharding data, etc.

Creating a database using “use” command

Creating a database in MongoDB is as simple as issuing the "using" command. The


following example shows how this can be done.

Code Explanation:

1. The "use" command is used to create a database in MongoDB. If the database


does not exist a new one will be created.

If the command is executed successfully, the following Output will be shown:

Output:
Air University, Islamabad
Department of Computer Science

MongoDB will automatically switch to the database once created.

Creating a Collection/Table using insert()

The easiest way to create a collection is to insert a record (which is nothing but a
document consisting of Field names and Values) into a collection. If the collection
does not exist a new one will be created.

The following example shows how this can be done.

db.Employee.insert
(
{
"Employeeid" : 1,
"EmployeeName" : "Martin"
}
)

Code Explanation:

As seen above, by using the "insert" command the collection will be created.

Adding documents using insert() command

MongoDB provides the insert () command to insert documents into a collection. The
following example shows how this can be done.

Step 1) Write the "insert" command


Air University, Islamabad
Department of Computer Science

Step 2) Within the "insert" command, add the required Field Name and Field Value
for the document which needs to be created.

Code Explanation:

1. The first part of the command is the "insert statement" which is the
statement used to insert a document into the collection.
2. The second part of the statement is to add the Field name and the Field value,
in other words, what is the document in the collection going to contain.

If the command is executed successfully, the following Output will be shown

Output:
Air University, Islamabad
Department of Computer Science

The output shows that the operation performed was an insert operation and that
one record was inserted into the collection.
Air University, Islamabad
Department of Computer Science

Add MongoDB Array using insert() with


Example
var myEmployee=
[

{
"Employeeid" : 1,
"EmployeeName" : "Smith"
},
{
"Employeeid" : 2,
"EmployeeName" : "Mohan"
},
{
"Employeeid" : 3,
"EmployeeName" : "Joe"
},

];

db.Employee.insert(myEmployee);

If the command is executed successfully, the following Output will be shown


Air University, Islamabad
Department of Computer Science

The output shows that those 3 documents were added to the collection.

Printing in JSON format


JSON is a format called JavaScript Object Notation, and is just a way to store
information in an organized, easy-to-read manner. In our further examples, we are
going to use the JSON print functionality to see the output in a better format.

Let's look at an example of printing in JSON format

db.Employee.find().forEach(printjson)
Code Explanation:

1. The first change is to append the function called for Each() to the find()
function. What this does is that it makes sure to explicitly go through each
document in the collection. In this way, you have more control of what you
can do with each of the documents in the collection.
Air University, Islamabad
Department of Computer Science

2. The second change is to put the printjson command to the forEach statement.
This will cause each document in the collection to be displayed in JSON
format.

If the command is executed successfully, the following Output will be shown

Output:

The output clearly shows that all of the documents are printed in JSON style.
Air University, Islamabad
Department of Computer Science

Mongodb Primary Key: Example to set _id field


with ObjectId()
What is Primary Key in MongoDB?
In MongoDB, _id field as the primary key for the collection so that each document
can be uniquely identified in the collection. The _id field contains a unique ObjectID
value.

By default when inserting documents in the collection, if you don't add a field name
with the _id in the field name, then MongoDB will automatically add an Object id
field as shown below

When you query the documents in a collection, you can see the ObjectId for each
document in the collection.

If you want to ensure that MongoDB does not create the _id Field when the
collection is created and if you want to specify your own id as the _id of the
collection, then you need to explicitly define this while creating the collection.

When explicitly creating an id field, it needs to be created with _id in its name.

Let's look at an example on how we can achieve this.

db.Employee.insert({_id:10, "EmployeeName" : "Smith"})


Air University, Islamabad
Department of Computer Science

Code Explanation:

1. We are assuming that we are creating the first document in the collection and
hence in the above statement while creating the collection, we explicitly
define the field _id and define a value for it.

If the command is executed successfully and now use the find command to display
the documents in the collection, the following Output will be shown

Output:

The output clearly shows that the _id field we defined while creating the collection is
now used as the primary key for the collection.
Air University, Islamabad
Department of Computer Science

Query Document using find() with Example


Basic query operations
The basic query operations cover the simple operations such as getting all of the
documents in a MongoDB collection. Let’s look at an example of how we can
accomplish this.

All of our code will be run in the MongoDB JavaScript command shell. Consider that
we have a collection named ‘Employee’ in our MongoDB database and we execute
the below command.

Code Explanation:

1. Employee is the collection name in the MongoDB database


2. The find command is an in-built function which is used to retrieve the
documents in the collection.

If the command is executed successfully, the following Output will be shown

Output:
Air University, Islamabad
Department of Computer Science

The output shows all the documents which are present in the collection.

We can also add criteria to our queries so that we can fetch documents based on
certain conditions.

Example 1
Let's look at a couple of examples of how we can accomplish this.

db.Employee.find({EmployeeName : "Smith"}).forEach(printjson);

Code Explanation:

1. Here we want to find for an Employee whose name is "Smith" in the collection
, hence we enter the filter criteria as EmployeeName : "Smith"

If the command is executed successfully, the following Output will be shown

Output:
Air University, Islamabad
Department of Computer Science

The output shows that only the document which contains "Smith" as the Employee
Name is returned.

Example 2
Now, let's take a look at another code example which makes use of the greater than
search criteria. When this criteria is included, it actually searches those documents
where the value of the field is greater than the specified value.

db.Employee.find({Employeeid : {$gt:2}}).forEach(printjson);

Code Explanation:

1. Here we want to find for all Employee's whose id is greater than 2. The $gt is
called a query selection operator, and what is just means is to use the greater
than expression.

If the command is executed successfully, the following Output will be shown

Output:
Air University, Islamabad
Department of Computer Science

All of the documents wherein the Employee id is greater than 2 is returned.


Air University, Islamabad
Department of Computer Science

What is Cursor in MongoDB?


When the db.collection.find () function is used to search for documents in the
collection, the result returns a pointer to the collection of documents returned which
is called a cursor.

By default, the cursor will be iterated automatically when the result of the query is
returned. But one can also explicitly go through the items returned in the cursor one
by one. If you see the below example, if we have 3 documents in our collection, the
cursor object will point to the first document and then iterate through all of the
documents of the collection.

The following example shows how this can be done.


Air University, Islamabad
Department of Computer Science

var myEmployee = db.Employee.find( { Employeeid : { $gt:2 }});

while(myEmployee.hasNext())

print(tojson(myEmployee.next()));

Code Explanation:

1. First we take the result set of the query which finds the Employee's whose id is
greater than 2 and assign it to the JavaScript variable 'myEmployee'
2. Next we use the while loop to iterate through all of the documents which are
returned as part of the query.
3. Finally for each document, we print the details of that document in JSON
readable format.

If the command is executed successfully, the following Output will be shown

Output:
Air University, Islamabad
Department of Computer Science

MongoDB order with Sort() & Limit() Query


with Examples
What is Query Modifications?
Mongo DB provides query modifiers such as the 'limit' and 'Orders' clause to provide
more flexibility when executing queries. We will take a look at the following query
modifiers

Limit Query Results


This modifier is used to limit the number of documents which are returned in the
result set for a query. The following example shows how this can be done.

db.Employee.find().limit(2).forEach(printjson);

Code Explanation:

1. The above code takes the find function which returns all of the documents in
the collection but then uses the limit clause to limit the number of documents
being returned to just 2.

Output:

If the command is executed successfully, the following Output will be shown


Air University, Islamabad
Department of Computer Science

The output clearly shows that since there is a limit modifier, so at most just 2 records
are returned as part of the result set based on the ObjectId in ascending order.

Sort by Descending Order


One can specify the order of documents to be returned based on ascending or
descending order of any key in the collection. The following example shows how this
can be done.

db.Employee.find().sort({Employeeid:-1}).forEach(printjson)

Code Explanation:

1. The above code takes the sort function which returns all of the documents in
the collection but then uses the modifier to change the order in which the
records are returned. Here the -1 indicates that we want to return the
documents based on the descending order of Employee id.

If the command is executed successfully, the following Output will be shown

Output:
Air University, Islamabad
Department of Computer Science

The output clearly shows the documents being returned in descending order of the
Employeeid.

Ascending order is defined by value 1.

Count() & Remove() Functions with Examples


The concept of aggregation is to carry out a computation on the results which are
returned in a query. For example, suppose you wanted to know what is the count of
documents in a collection as per the query fired, then MongoDB provides the count()
function.

Let's look at an example of this.

db.Employee.count()

Code Explanation:

1. The above code executes the count function.

If the command is executed successfully, the following Output will be shown

Output:
Air University, Islamabad
Department of Computer Science

The output clearly shows that 4 documents are there in the collection.

Performing Modifications

The other two classes of operations in MongoDB are the update and remove
statements.

The update operations allow one to modify existing data, and the remove operations
allow the deletion of data from a collection.

Deleting Documents
In MongoDB, the db.collection.remove () method is used to remove documents
from a collection. Either all of the documents can be removed from a collection or
only those which matches a specific condition.

If you just issue the remove command, all of the documents will be removed from
the collection.

The following code example demonstrate how to remove a specific document from
the collection.

db.Employee.remove({Employeeid:22})

Code Explanation:

1. The above code use the remove function and specifies the criteria which in
this case is to remove the documents which have the Employee id as 22.
Air University, Islamabad
Department of Computer Science

If the command is executed successfully, the following Output will be shown

Output:

The output will show that 1 document was modified.

Update() Document with Example


Step 1) Issue the update command

Step 2) Choose the condition which you want to use to decide which document
needs to be updated. In our example, we want to update the document which has
the Employee id 22.

Step 3) Use the set command to modify the Field Name

Step 4) Choose which Field Name you want to modify and enter the new value
accordingly.

db.Employee.update(
{"Employeeid" : 1},
{$set: { "EmployeeName" : "NewMartin"}});

If the command is executed successfully, the following Output will be shown


Air University, Islamabad
Department of Computer Science

Output:

The output clearly shows that one record matched the condition and hence the
relevant field value was modified.
Air University, Islamabad
Department of Computer Science

Updating Multiple Values


Step 1) Issue the update command

Step 2) Choose the condition which you want to use to decide which document
needs to be updated. In our example, we want the document which has the
Employee id of "1" to be updated.

Step 3) Choose which Field Name's you want to modify and enter their new value
accordingly.

db.Employee.update
(
{
Employeeid : 1
},
{
$set :
{
"EmployeeName" : "NewMartin",
"Employeeid" : 22
}
}
)

If the command is executed successfully and if you run the "find" command to
search for the document with Employee id as 22 you will see the following Output
will be shown

Output:
Air University, Islamabad
Department of Computer Science

The output clearly shows that one record matched the condition and hence the
relevant field value was modified.
Air University, Islamabad
Department of Computer Science

Indexing - createIndex(), dropindex() Example


Understanding Impact of Indexes
Now even though from the introduction we have seen that indexes are good for
queries, but having too many indexes can slow down other operations such as the
Insert, Delete and Update operation.

If there are frequent insert, delete and update operations carried out on documents,
then the indexes would need to change that often, which would just be an overhead
for the collection.

The below example shows an example of what field values could constitute an index
in a collection. An index can either be based on just one field in the collection, or it
can be based on multiple fields in the collection.

In the example below, the Employeeid "1" and EmployeeCode "AA" are used to index
the documents in the collection. So when a query search is made, these indexes will
be used to quickly and efficiently find the required documents in the collection.

So even if the search query is based on the EmployeeCode "AA", that document
would be returned.
Air University, Islamabad
Department of Computer Science

How to Create Indexes: createIndex()


Creating an Index in MongoDB is done by using the "createIndex" method.

The following example shows how add index to collection. Let's assume that we have
our same Employee collection which has the Field names of "Employeeid" and
"EmployeeName".

db.Employee.createIndex({Employeeid:1})

Code Explanation:

1. The createIndex method is used to create an index based on the "Employeeid"


of the document.
2. The '1' parameter indicates that when the index is created with the
"Employeeid" Field values, they should be sorted in ascending order. Please
note that this is different from the _id field (The id field is used to uniquely
identify each document in the collection) which is created automatically in the
collection by MongoDB. The documents will now be sorted as per the
Employeeid and not the _id field.

If the command is executed successfully, the following Output will be shown:

Output:
Air University, Islamabad
Department of Computer Science

1. The numIndexesBefore: 1 indicates the number of Field values (The actual


fields in the collection) which were there in the indexes before the command
was run. Remember that each collection has the _id field which also counts as
a Field value to the index. Since the _id index field is part of the collection
when it is initially created, the value of numIndexesBefore is 1.
2. The numIndexesAfter: 2 indicates the number of Field values which were
there in the indexes after the command was run.
3. Here the "ok: 1" output specifies that the operation was successful, and the
new index is added to the collection.

The above code shows how to create an index based on one field value, but one can
also create an index based on multiple field values.

The following example shows how this can be done;


Air University, Islamabad
Department of Computer Science

db.Employee.createIndex({Employeeid:1, EmployeeName:1])

Code Explanation:

1. The createIndex method now takes into account multiple Field values which
will now cause the index to be created based on the "Employeeid" and
"EmployeeName". The Employeeid:1 and EmployeeName:1 indicates that the
index should be created on these 2 field values with the :1 indicating that it
should be in ascending order.

How to Find Indexes: getindexes()


Finding an Index in MongoDB is done by using the "getIndexes" method.

The following example shows how this can be done;

db.Employee.getIndexes()

Code Explanation:

1. The getIndexes method is used to find all of the indexes in a collection.

If the command is executed successfully, the following Output will be shown:


Air University, Islamabad
Department of Computer Science

Output:

1. The output returns a document which just shows that there are 2 indexes in
the collection which is the _id field, and the other is the Employee id field. The
:1 indicates that the field values in the index are created in ascending order.

How to Drop Indexes: dropindex()


Removing an Index in MongoDB is done by using the dropIndex method.

The following example shows how this can be done;

db.Employee.dropIndex(Employeeid:1)
Air University, Islamabad
Department of Computer Science

Code Explanation:

1. The dropIndex method takes the required Field values which needs to be
removed from the Index.

If the command is executed successfully, the following Output will be shown:

Output:

1. The nIndexesWas: 3 indicates the number of Field values which were there in
the indexes before the command was run. Remember that each collection has
the _id field which also counts as a Field value to the index.
2. The ok: 1 output specifies that the operation was successful, and the
"Employeeid" field is removed from the index.

To remove all of the indexes at once in the collection, one can use the dropIndexes
command.

The following example shows how this can be done.


Air University, Islamabad
Department of Computer Science

db.Employee.dropIndex()

Code Explanation:

1. The dropIndexes method will drop all of the indexes except for the _id index.

If the command is executed successfully, the following Output will be shown:

Output:

1. The nIndexesWas: 2 indicates the number of Field values which were there in
the indexes before the command was run.
2. Remember again that each collection has the _id field which also counts as a
Field value to the index, and that will not be removed by MongoDB and that is
what this message indicates.
3. The ok: 1 output specifies that the operation was successful.

Summary
Air University, Islamabad
Department of Computer Science

 Defining indexes are important for faster and efficient searching of documents
in a collection.
 Indexes can be created by using the createIndex method. Indexes can be
created on just one field or multiple field values.
 Indexes can be found by using the getIndexes method.
 Indexes can be removed by using the dropIndex for single indexes or
dropIndexes for dropping all indexes.

MongoDB Regular Expression (Regex) with


Examples
Using $regex operator for Pattern matching
The regex operator in MongoDB is used to search for specific strings in the collection.
The following example shows how this can be done.

Let's assume that we have our same Employee collection which has the Field names
of "Employeeid" and "EmployeeName". Let' also assume that we have the following
documents in our collection.

Here in the below code we have used regex operator to specify the search criteria.
Air University, Islamabad
Department of Computer Science

db.Employee.find({EmployeeName : {$regex: "Gu" }}).forEach(printjson)

Code Explanation:

1. Here we want to find all Employee Names which have the characters 'Gu' in it.
Hence, we specify the $regex operator to define the search criteria of 'Gu'
2. The printjson is being used to print each document which is returned by the
query in a better way.

If the command is executed successfully, the following Output will be shown:

Output:

The output clearly shows that those documents wherein the Employee Name
contains the 'Gu' characters are returned.

If suppose your collection has the following documents with an additional document
which contained the Employee Name as "Guru999". If you entered the search
criteria as "Guru99", it would also return the document which had "Guru999". But
suppose if we didn't want this and only wanted to return the document with
"Guru99". Then we can do this with exact pattern matching. To do an exact pattern
Air University, Islamabad
Department of Computer Science

matching, we will use the ^ and $ character. We will add the ^ character in the
beginning of the string and $ at the end of the string.

The following example shows how this can be done.

db.Employee.find({EmployeeName : {$regex: "^Guru99$"}}).forEach(printjson)

Code Explanation:

1. Here in the search criteria, we are using the ^ and $ character. The ^ is used to
make sure that the string starts with a certain character, and $ is used to
ensure that the string ends with a certain character. So when the code
executes it will fetch only the string with the name "Guru99".
2. The printjson is being used to print each document which is returned by the
query in a better way.

If the command is executed successfully, the following Output will be shown:


Air University, Islamabad
Department of Computer Science

Output:

In the output, it is clearly visible that string "Guru99" is fetched.

Pattern Matching with $options


When using the regex operator one can also provide additional options by using
the $options keyword. For example, suppose you wanted to find all the documents
which had 'Gu' in their Employee Name, irrespective of whether it was case sensitive
or insensitive. If such a result is desired, then we need to use the $options with case
insensitivity parameter.

The following example shows how this can be done.

Let's assume that we have our same Employee collection which has the Field names
of "Employeeid" and "EmployeeName".

Let' also assume that we have the following documents in our collection.
Air University, Islamabad
Department of Computer Science

Now if we run the same query as in the last topic, we would never see the document
with "GURU99" in the result. To ensure this comes in the result set, we need to add
the $options "I" parameter.

db.Employee.find({EmployeeName:{$regex: "Gu",$options:'i'}}).forEach(printjson)

Code Explanation:

1. The $options with 'I' parameter (which means case insensitivity) specifies that
we want to carry out the search no matter if we find the letters 'Gu' in lower
or upper case.

If the command is executed successfully, the following Output will be shown:

Output:

1. The output clearly shows that even though one document has the upper case
'Gu' , the document still gets displayed in the result set.
Air University, Islamabad
Department of Computer Science

Pattern matching without the regex operator


One can also do pattern matching without the regex operator. The following
example shows how this can be done.

db.Employee.find({EmployeeName: /Gu/'}).forEach(printjson)

Code Explanation:

1. The "//" options basically means to specify your search criteria within these
delimiters. Hence, we are specifying /Gu/ to again find those documents
which have 'Gu' in their EmployeeName.

If the command is executed successfully, the following Output will be shown:

Output:

The output clearly shows that those documents wherein the Employee Name
contains the 'Gu' characters are returned.
Air University, Islamabad
Department of Computer Science

Fetching last 'n' documents from a collection


There are various ways to get the last n documents in a collection.

Let's look at one of the ways via the following steps

The following example shows how this can be done.

Let's assume that we have our same Employee collection which has the Field names
of "Employeeid" and "EmployeeName".

Let' also assume that we have the following documents in our collection:

db.Employee.find().sort({_id:-1}).limit(2).forEach(printjson)

Code Explanation:
Air University, Islamabad
Department of Computer Science

1) When querying for the documents, use the sort function to sort the records in
reverse order based on the _id field value in the collection. The -1 basically indicates
to sort the documents in reverse order or descending order so that the last
document becomes the first document to be displayed.

2) Then use the limit clause to just display the number of records you want. Here we
have set the limit clause (2), so it will fetch the last two documents.

If the command is executed successfully, the following Output will be shown:

Output:

The output clearly shows that the last two documents in the collection are displayed.
Hence we have clearly shown that to fetch the last 'n' documents in the collection,
we can first sort the documents in descending order and then use the limit clause to
return the 'n' number of documents which are required.

Note: If the search is performed on a string which is greater than say 38,000
characters, it will not display the right results.

Summary:

 Pattern matching can be achieved by the $regex operator. This operator can
be used to find for certain strings in the collection.
 The ^ and $ symbol can be used for exact text searches with ^ being used to
make sure that the string starts with a certain character and $ used to ensure
that the string ends with a certain character.
 The 'i' along with the $regex operator can be used to specify case insensitivity
so that strings can be searched whether they are in lower case or upper case.
Air University, Islamabad
Department of Computer Science

 The delimiters // can also be used for pattern matching.


 Use a combination of sort and the limit function to return the last n
documents in the collection. The sort function can be used to return the
documents in descending order after which the limit clause can be used to
limit the number of documents being returned.

You might also like