Lab - MongoDB P II PDF
Lab - MongoDB P II PDF
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.
Code Explanation:
Output:
Air University, Islamabad
Department of Computer Science
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.
db.Employee.insert
(
{
"Employeeid" : 1,
"EmployeeName" : "Martin"
}
)
Code Explanation:
As seen above, by using the "insert" command the collection will be created.
MongoDB provides the insert () command to insert documents into a collection. The
following example shows how this can be done.
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.
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
{
"Employeeid" : 1,
"EmployeeName" : "Smith"
},
{
"Employeeid" : 2,
"EmployeeName" : "Mohan"
},
{
"Employeeid" : 3,
"EmployeeName" : "Joe"
},
];
db.Employee.insert(myEmployee);
The output shows that those 3 documents were added to the collection.
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.
Output:
The output clearly shows that all of the documents are printed in JSON style.
Air University, Islamabad
Department of Computer Science
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.
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
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:
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"
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.
Output:
Air University, Islamabad
Department of Computer Science
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.
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.
Output:
Air University, Islamabad
Department of Computer Science
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:
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.
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.
Output:
Air University, Islamabad
Department of Computer Science
The output clearly shows the documents being returned in descending order of the
Employeeid.
db.Employee.count()
Code Explanation:
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
Output:
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 4) Choose which Field Name you want to modify and enter the new value
accordingly.
db.Employee.update(
{"Employeeid" : 1},
{$set: { "EmployeeName" : "NewMartin"}});
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
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
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
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:
Output:
Air University, Islamabad
Department of Computer Science
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.
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.
db.Employee.getIndexes()
Code Explanation:
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.
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.
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.
db.Employee.dropIndex()
Code Explanation:
1. The dropIndexes method will drop all of the indexes except for the _id index.
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.
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
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.
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.
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.
Output:
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.
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
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.
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
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.
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