Lab Praticals Adbms 22-23
Lab Praticals Adbms 22-23
LABORATORY MANAUL
On
“SOFTWARE LABORATORY – I”
SEMESTER – (I)
A.Y. 2019-20
1
SR. TITLE PAGE DATE SIGN
NO. NO.
Group C: MongoDB
1. Create a database with suitable example using MongoDB and
implement
1. Inserting and saving document (batch insert, insert validation)
2. Removing document
3. Updating document (document replacement, using modifiers,
upserts, updating multiple documents, returning updated documents)
2. Execute at least 10 queries on any suitable MongoDB database that
demonstrates following querying techniques:
1. find and findOne (specific values)Query criteria (Query
conditionals, OR queries, $not, Conditional semantics)
2. Type-specific queries (Null, Regular expression, Querying arrays)
3. Execute at least 10 queries on any suitable MongoDB database that
demonstrates following:
1. $ where queries
2. Cursors (Limits, skips, sorts, advanced query options)
3. Database commands
4. Implement Map reduce example with suitable example.
2
Department of Information Technology, KBTCOE
3
Experiment No: Group_C_01 Date:
1.1 Aim: Create a database with suitable example using MongoDB and implement
1. Inserting and saving document (batch insert, insert validation)
2. Removing document
3. Updating document (document replacement, using modifiers, upserts,
updating multiple documents, returning updated documents)
1.2 Objectives: 1. To learn the concept of MongoDB.
2. Learn to access the data from MongoDB.
1.3 Hardware used: Computer System
1.4 Softwares used / Programming Languages Used:
Open source operating system (Linux).
MongoDB.
1.5. Theory:
Introduction:
MongoDB is a cross-platform, document oriented database that provides, high
performance, high availability, and easy scalability. MongoDB works on concept of
collection and document.
Database
Database is a physical container for collections. Each database gets its own set of files
on the file system. A single MongoDB server typically has multiple databases.
Collection
Collection is a group of MongoDB documents. It is the equivalent of an RDBMS
table. A collection exists within a single database. Collections do not enforce a
schema. Documents within a collection can have different fields. Typically, all
documents in a collection are of similar or related purpose.
Document
A document is a set of key-value pairs. Documents have dynamic schema. Dynamic
schema means that documents in the same collection do not need to have the same set
of fields or structure, and common fields in a collection's documents may hold
different types of data. To create a database in MongoDB use following commands:
The use Command
MongoDB use DATABASE_NAME is used to create database. The command will
create a new database if it doesn't exist, otherwise it will return the existing database.
Department of Information Technology, KBTCOE
Syntax
Basic syntax of use DATABASE statement is as follows,
use DATABASE_NAME
Example
If you want to create a database with name <mydb>, then use DATABASE statement
would be as follows,
>use mydb
switched to db mydb
To check your currently selected database, use the command db
>db
mydb
If you want to check your databases list, use the command show dbs.
>show dbs
local 0.78125GB
test 0.23012GB
Your created database (mydb) is not present in list. To display database, you need to
insert at least one document into it.
>db.movie.insert({"name":"tutorials point"})
>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
The drop Database() Method
MongoDB db.dropDatabase() command is used to drop a existing database.
Syntax
Basic syntax of dropDatabase() command is as follows −
db.dropDatabase()
This will delete the selected database. If you have not selected any database, then it
will delete default 'test' database.
Example
First, check the list of available databases by using the command, show dbs.
>show dbs
local 0.78125GB
5|6 5
Department of Information Technology, KBTCOE
mydb 0.23012GB
test 0.23012GB
If you want to delete new database <mydb>, then dropDatabase() command would
be as follows −
>use mydb
switched to db mydb
>db.dropDatabase()
>{ "dropped" : "mydb", "ok" : 1 }
Now check list of databases.
>show dbs
local 0.78125GB
test 0.23012GB
The createCollection() Method
MongoDB db.createCollection(name, options) is used to create collection.
Syntax
Basic syntax of createCollection() command is as follows −
db.createCollection(name, options)
In the command, name is name of collection to be created. Options is a document
and is used to specify configuration of collection.
The insert() Method
To insert data into MongoDB collection, you need to use MongoDB's insert () or
save () method.
Syntax
The basic syntax of insert () command is as follows,
>db.COLLECTION_NAME.insert(document)
Example
>db.mycol.insert({
_id: ObjectId(7df78ad8902c),
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
6|6 5
Department of Information Technology, KBTCOE
likes: 100
})
The find() Method
To query data from MongoDB collection, you need to use MongoDB's find() method.
Syntax
The basic syntax of find() method is as follows −
>db.COLLECTION_NAME.find()
find() method will display all the documents in a non-structured way.
The pretty () Method
To display the results in a formatted way, you can use pretty () method.
Syntax
>db.mycol.find().pretty()
>db.mycol.find().pretty()
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "tutorials point",
"url": "http://www.tutorialspoint.com",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
The remove () Method
MongoDB's remove () method is used to remove a document from the collection.
remove () method accepts two parameters. One is deletion criteria and second is
justOne flag.
• deletion criteria − (Optional) deletion criteria according to documents will be
removed.
• justOne − (Optional) if set to true or 1, then remove only one document.
Syntax
Basic syntax of remove () method is as follows −
>db.COLLECTION_NAME.remove (DELLETION_CRITTERIA)
MongoDB's update () and save () methods are used to update document into a
collection. The update () method updates the values in the existing document while
7|6 5
Department of Information Technology, KBTCOE
the save() method replaces the existing document with the document passed in save()
method.
MongoDB Update () Method
The update () method updates the values in the existing document.
Syntax
The basic syntax of update() method is as follows −
>db.COLLECTION_NAME.update(SELECTION_CRITERIA,
UPDATED_DATA)
1.6 Conclusion: In this way we can study various SQL commands. All these
commands (DDL, DCL and DML) are used to access the database information.
1.7 Questions
1. What is MongoDB?
2. Enlist the features of MongoDB?
3. What are different components of MongoDB?
8|6 5
Department of Information Technology, KBTCOE
9|6 5
Department of Information Technology, KBTCOE
10 | 6 5
Department of Information Technology, KBTCOE
{ "_id" : 901 }
])
The { name : null } query matches documents that either contain the name field
whose value is null or that do not contain the name field.
Given the following query:
db.users.find( { name: null } )
The query returns both documents:
{ "_id" : 900, "name" : null }
{ "_id" : 901 }
Regular Expression
MongoDB provides functionality of regular expression for string pattern matching
using the $regex operator. MongoDB uses PCRE (Perl Compatible Regular
Expression) as regular expression language. Unlike text search, we do not need to do
any configuration or command to use regular expressions.
Consider the following document structure under posts collection containing the post
text and its tags −
{
"post_text": "enjoy the mongodb articles on tutorialspoint",
"tags": [
"mongodb",
"tutorialspoint"
]}
Using regex Expression
The following regex query searches for all the posts containing string tutorialspoint
in it −
>db.posts.find({post_text:{$regex:"tutorialspoint"}})
The same query can also be written as −
>db.posts.find({post_text:/tutorialspoint/})
The pretty() Method
To display the results in a formatted way, you can use pretty() method.
>db.mycol.find().pretty()
11 | 6 5
Department of Information Technology, KBTCOE
12 | 6 5
Department of Information Technology, KBTCOE
13 | 6 5
Department of Information Technology, KBTCOE
assign the cursor returned from the find() method to a variable using the var keyword,
the cursor does not automatically iterate. We can call the cursor variable in the shell
to iterate up to 20 times and print the matching documents, as in the following
example:
var myCursor = db.users.find( { type: 2 } );
myCursor
You can also use the cursor method next() to access the documents, as in the below
example:
var myCursor = db.users.find( { type: 2 } );
while (myCursor.hasNext()) {
print(tojson(myCursor.next()));
}
As an alternative print operation, consider the printjson() helper method to replace
print(tojson()):
var myCursor = db.users.find( { type: 2 } );
while (myCursor.hasNext()) {
printjson(myCursor.next());
}
We can use the cursor method for Each() to iterate the cursor and access the
documents, as in the following example:
var myCursor = db.users.find( { type: 2 } );
myCursor.forEach(printjson);
cursor.limit()
We can use the limit() method on a cursor to specify the maximum number of
documents the cursor will return. limit() is analogous to the LIMIT statement in a
SQL database. Use limit() to maximize performance and prevent MongoDB from
returning more results than required for processing.
The cursor.limit () method has the following prototype form:
db.collection.find (<query>).limit(<number>)
skip (aggregation)
We can skips over the specified number of documents that pass into the stage and
passes the remaining documents to the next stage in the pipeline.
The $skip stage has the following prototype form:
14 | 6 5
Department of Information Technology, KBTCOE
{ $skip: <positive integer> }
$skip takes a positive integer that specifies the maximum number of documents to
skip.
Example
Consider the following example:
db.article.aggregate(
{ $skip : 5 }
);
cursor.skip()
We can call the cursor.skip() method on a cursor to control where MongoDB begins
returning results. This approach may be useful in implementing “paged” results.
(We must apply cursor.skip() to the cursor before retrieving any documents from the
database.)
Consider the following JavaScript function as an example of the skip function:
function printStudents(pageNumber, nPerPage) {
print("Page: " + pageNumber);
db.students.find().skip(pageNumber > 0 ? ((pageNumber-1)*nPerPage) :
0).limit(nPerPage).forEach( function(student) { print(student.name + "<p>"); } );
}
The cursor.skip() method is often expensive because it requires the server to walk
from the beginning of the collection or index to get the offset or skip position before
beginning to return results. As the offset (e.g. pageNumber above) increases,
cursor.skip() will become slower and more CPU intensive. With larger collections,
cursor.skip() may become IO bound.
cursor.sort()
It specifies the order in which the query returns matching documents. We must apply
sort() to the cursor before retrieving any documents from the database.
The sort() method has the following parameter:
sort document A document that defines the sort order of the result set.
15 | 6 5
Department of Information Technology, KBTCOE
The sort parameter contains field and value pairs, in the following form:
{ field: value }
Result Ordering
Unless we specify the sort() method or use the $near operator, MongoDB does not
guarantee the order of query results.
Ascending/Descending Sort
Specify in the sort parameter the field or fields to sort by and a value of 1 or -1 to
specify an ascending or descending sort respectively. The following sample document
specifies a descending sort by the age field and then an ascending sort by the posts
field:
{ age : -1, posts: 1 }
When comparing values of different BSON types, MongoDB uses the following
comparison order, from lowest to highest:
1. MinKey (internal type)
2. Null
3. Numbers (ints, longs, doubles, decimals)
4. Symbol, String
5. Object
6. Array
7. BinData
8. ObjectId
9. Boolean
10. Date
11. Timestamp
12. Regular Expression
13. MaxKey (internal type)
Database Commands
All command documentation given below describes a command and its available
parameters and provides a document template or prototype for each command. Some
command documentation also includes the relevant mongo shell helpers.
To run a command, use the db.runCommand():
db.runCommand( { <command> } )
16 | 6 5
Department of Information Technology, KBTCOE
User Commands
Aggregation Commands
Name Description
aggregate Performs aggregation tasks such as group using the aggregation
framework.
count Counts the number of documents in a collection.
distinct Displays the distinct values found for a specified key in a
collection.
group Deprecated. Groups documents in a collection by the specified
key and performs simple aggregation.
mapReduce Performs map-reduce aggregation for large data sets.
Geospatial Commands
Name Description
geoNear Performs a geospatial query that returns the documents closest
to a given point.
geoSearch Performs a geospatial query that uses MongoDB’s haystack
index functionality
17 | 6 5
Department of Information Technology, KBTCOE
1.6 Conclusion: In this way we can study cursors in mongodb. Cursors can be
applied to databases for specific purposes. We can also use various database
commands for different operations.
1.7 Questions
1. What are where queries in mongodb?
2. What are cursors?
3. What are different database commands?
18 | 6 5
Department of Information Technology, KBTCOE
19 | 6 5
Department of Information Technology, KBTCOE
20 | 6 5
Department of Information Technology, KBTCOE
},
"ok" : 1,
}
The result shows that a total of 4 documents matched the query (status:"active"), the
map function emitted 4 documents with key-value pairs and finally the reduce
function grouped mapped documents having the same keys into 2.
To see the result of this mapReduce query, use the find operator −
>db.posts.mapReduce(
function() { emit(this.user_id,1); },
function(key, values) {return Array.sum(values)}, {
query:{status:"active"},
out:"post_total"
}
).find()
The above query gives the following result which indicates that both user’s tom and
mark have two posts in active states-
{ "_id" : "tom", "value" : 2 }
{ "_id" : "mark", "value" : 2 }
In a similar manner, MapReduce queries can be used to construct large complex
aggregation queries. The use of custom Javascript functions make use of MapReduce
which is very flexible and powerful.
1.6 Conclusion: In this way we can study MapReduce function for handling large
data.
1.7 Questions
1. What is BigData?
2. What MapReduce function?
21 | 6 5
Department of Information Technology, KBTCOE
22 | 6 5
Department of Information Technology, KBTCOE
likes: 100
},
{
_id: ObjectId(7df78ad8902d)
title: 'NoSQL Overview',
by_user: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 10
},
{
_id: ObjectId(7df78ad8902e)
title: 'Neo4j Overview',
description: 'Neo4j is no sql database',
by_user: 'Neo4j',
url: 'http://www.neo4j.com',
tags: ['neo4j', 'database', 'NoSQL'],
likes: 750
},
Now from the above collection, if we want to display a list stating how many tutorials
are written by each user, then we will use the following aggregate() method −
> db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}])
{
"result" : [
{
"_id" : "tutorials point",
"num_tutorial" : 2
},
{
"_id" : "Neo4j",
"num_tutorial" : 1
23 | 6 5
Department of Information Technology, KBTCOE
}
],
"ok" : 1
}
Sql equivalent query for the above use case will be select by_user, count(*) from
mycol group by by_user.
In the above example, we have grouped documents by field by_user and on each
occurrence of by_user previous value of sum is incremented.
Indexing
Indexes support the efficient resolution of queries. Without indexes, MongoDB must
scan every document of a collection to select those documents that match the query
statement. This scan is highly inefficient and requires MongoDB to process a large
volume of data. Indexes are special data structures, that store a small portion of the
data set in an easy-to-traverse form. The index stores the value of a specific field or
set of fields, ordered by the value of the field as specified in the index.
The ensureIndex() Method
To create an index you need to use ensureIndex() method of MongoDB.
Syntax
The basic syntax of ensureIndex() method is as follows().
>db.COLLECTION_NAME.ensureIndex({KEY:1})
Here key is the name of the field on which we want to create index and 1 is for
ascending order. To create index in descending order we need to use -1.
Example
>db.mycol.ensureIndex({"title":1})
In ensureIndex() method we can pass multiple fields, to create index on multiple
fields.
>db.mycol.ensureIndex({"title":1,"description":-1})
Index Creation
MongoDB provides several options to create indexes. By default, when indexes are
created, all other operations on a database are blocked. For example, when indexes on
24 | 6 5
Department of Information Technology, KBTCOE
a collection are created, the database becomes unavailable for any read or writes
operation until the index creation process completes. The read or write operations on
the database queue and allow the index building process to complete. Therefore, for
index building operations which may consume longer time, we can consider the
background operation and thus make MongoDB available even during the entire
operation. The command given on the screen is used for this purpose. By default,
background is false for building MongoDB indexes.
When MongoDB is creating indexes in the background for a collection, we cannot
perform other administrative operations involving that collection. For example, we
cannot perform tasks, such as runrepair Database, (read as run repair database) drop
the collection, or use the query db.collection.drop(),(read as D-B dot collection dot
drop) and runcompact (read as run compact). If we perform any of these operations,
we will receive an error. The index build process at the background uses an
incremental approach and is slower than the normal “foreground” index build process.
The speed of the index build process depends on the size of the index. If the index
size is bigger than the RAM of the system, the process takes more time than the
foreground process. Building indexes can impact database performance: • If the
application includes createIndex()(read as create index) operations and • If no index is
available for operational concerns. To avoid any performance issues, we can use the
getIndexes()(read as det indexes) method to ensure that your application checks for
the indexes at the start up. We can also use an equivalent method for your driver and
ensure it terminates an operation if the proper indexes do not exist. When building
indexes, use separate application codes and designated maintenance windows.
Remove Indexes
We can use the following methods to remove indexes.
dropIndex() (read as drop index) method: This removes an index from a collection.
db.collection.dropIndex() (read as D-B dot collection dot drop index) method: This
removes an index. For example, the first operation given on the screen removes an
ascending index on the item field in the items collection. To remove all indexes
barring the _id index from a collection, use the second operation provided on the
screen.
25 | 6 5
Department of Information Technology, KBTCOE
1.6 Conclusion: In this way we can the concept of aggregation and indexing in
mongodb.
1.7 Questions
1. What is aggregation in mongodb?
2. What is indexing in mongodb?
26 | 6 5
Department of Information Technology, KBTCOE
27 | 6 5
Department of Information Technology, KBTCOE
28 | 6 5
Department of Information Technology, KBTCOE
29 | 6 5
Department of Information Technology, KBTCOE
Syntax
{key:[{value1},{value2},…..]}
Example showing an Array Object:
{ "books": [ { "language":"Java" , "edition":"second" },
{ "language":"C++" , "lastName":"fifth" },
{ "language":"C" , "lastName":"third" } ] }
Sample Example:
<html>
<head>
<title>Creation of array object in javascript using JSON</title>
<script language="javascript" >
document.writeln("<h1>Example of JSON Array object</h1>");
var book = { "DBMS" : [
{ "Name" : "DBMS System", "Price" : 250 },
{ "Name" : "No SQL ", "price" : 400 }
],
"Mongo" : [
{ "Name" : "Mongo DB", "price" : 200 },
{ "Name" : "Mongo DB and Java", "price" : 300 }
]
}
var i = 0
document.writeln("<table border='4'><tr>");
for(i=0;i<book.DBMS.length;i++)
{
document.writeln("<td>");
document.writeln("<table border='2' width=100 >");
document.writeln("<tr><td><b>Name</b></td><td width=50>"
+ book.DBMS[i].Name+"</td></tr>");
document.writeln("<tr><td><b>Price</b></td><td width=50>"
+ book.DBMS[i].price +"</td></tr>");
document.writeln("</table>");
document.writeln("</td>");
30 | 6 5
Department of Information Technology, KBTCOE
}
for(i=0;i<book.Mongo.length;i++)
{
document.writeln("<td>");
document.writeln("<table border='2' width=100 >");
document.writeln("<tr><td><b>Name</b></td><td width=50>"
+ book.Mongo[i].Name+"</td></tr>");
document.writeln("<tr><td><b>Price</b></td><td width=50>"
+ book.Mongo[i].price+"</td></tr>");
document.writeln("</table>");
document.writeln("</td>");
}
document.writeln("</tr></table>");
</script>
</head>
<body>
</body>
</html>
1.6 Conclusion: In this way, we have studied how to Create simple JSON objects and
write the concluding remarks specifying the use of JSON.
1.7 Questions:
1. Explain the use of JSON.
2. Explain encoding and decoding functions in JSON.
3. How to encode/decode JSON objects using PHP? Explain with suitable example?
4. How to encode/decode JSON objects using JAVA? Explain with suitable example?
31 | 6 5
Department of Information Technology, KBTCOE
32 | 6 5
Department of Information Technology, KBTCOE
$e = new Emp();
$e->name = "sachin";
$e->hobbies = "sports";
$e->birthdate = date('m/d/Y h:i:s a', strtotime("8/5/1974 11:20:03"));
echo json_encode($e);?>
can be encoded to JSON object
{"name":"sachin","hobbies":"sports","birthdate":"08/05/1974 11:20:03 pm"}
Decoding:
json_decode () function is used for decoding JSON object in to PHP.
Syntax:
json_decode ($json [,$assoc = false [, $depth = 511 [, $options = 0 ]]])
Parameters:
json_string : It is encoded string which must be UTF-8 encoded data.
assoc : It is a boolean type parameter, when set to TRUE, returned objects
will be converted into associative arrays.
depth: It is an integer type parameter which specifies recursion depth
options: It is an integer type bitmask of JSON decode. It supports
JSON_BIGINT_AS_STRING
Example:
The following JSON object
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>
can be decoded into
object(stdClass)#1 (5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
33 | 6 5
Department of Information Technology, KBTCOE
array(5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
1.6 Conclusion: Write your analysis (whatever you have learned) for this assignment
as concluding points.
1.7 Questions:
1. Implement encoding/decoding of JSON objects using JAVA.
2. Define encoding and decoding of objects in general.
3. Whether encoding/decoding supports simple JSON objects or JSON Array objects
or both.
Explain with suitable example.
34 | 6 5