0% found this document useful (0 votes)
18 views34 pages

Lab Praticals Adbms 22-23

The document is a laboratory manual for 'Software Laboratory - I' at KBTCOE, detailing various MongoDB operations and experiments for students. It includes instructions for creating databases, executing queries, and implementing features like aggregation and indexing. The manual also covers practical applications using JSON and programming languages such as Java, Perl, and Python.

Uploaded by

Nitin Tupsamudre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views34 pages

Lab Praticals Adbms 22-23

The document is a laboratory manual for 'Software Laboratory - I' at KBTCOE, detailing various MongoDB operations and experiments for students. It includes instructions for creating databases, executing queries, and implementing features like aggregation and indexing. The manual also covers practical applications using JSON and programming languages such as Java, Perl, and Python.

Uploaded by

Nitin Tupsamudre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

A

LABORATORY MANAUL
On

“SOFTWARE LABORATORY – I”

SEMESTER – (I)

Prepared by: Mrs. Bhawana A. Ahire

NAME OF LABORATORY: SOFTWARE LABORATORY-I


DEPARTMENT OF INFORMATION AND TECHNOLOGY

Maratha Vidya Prasarak Samaj’s Karmaveer Adv. Baburao


Ganpatrao Thakare College of Engineering Nashik

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.

5. Implement the aggregation and indexing with suitable example in


MongoDB. Demonstrate the following:
1. Aggregation framework
2. Create and drop different types of indexes and explain to show the
advantage of the indexes.
Group D: Mini Project / Database Application Development
Group E : On and Above Practical’s i.e. Beyond Syllabus
1. Create simple objects and array objects using JSON.
2. Encode and Decode JSON objects using Java/Perl/PHP/Python/Ruby

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?

Signature of Staff with Date

8|6 5
Department of Information Technology, KBTCOE

Experiment No: Group_C_02 Date:


1.1 Aim: Execute at least 10 queries on any suitable MongoDB database that
demonstrates following querying techniques:
1. find and findOne (specific values)
2. Query criteria (Query conditionals, OR queries, $not, Conditional semantics)
3. Type-specific queries (Null, Regular expression, Querying arrays).
1.2 Objectives: 1. To learn various MongoDB operations to access data from mongo
Database.
1.3 Hardware used: Computer System
1.4 Softwares used / Programming Languages Used:
Open source operating system (Linux).
MongoDB.
1.5. Theory:
Introduction:
Mongodb gives different operations to access data from mongo database. Following
are some of the operations.
db.collection.find()
db.collection.find (query, projection)
Selects documents in a collection and returns a cursor to the selected documents.
Parameter Type Description
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.
db.collection.findOne()
db.collection.findOne(query, projection)
This returns one document that satisfies the specified query criteria. If multiple
documents satisfy the query, this method returns the first document according to the
natural order which reflects the order of documents on the disk. In capped collections,
natural order is the same as insertion order. If no document satisfies the query, the
method returns null.

9|6 5
Department of Information Technology, KBTCOE

Parameter Type Description


query document Optional. Specifies query selection criteria using
query operators.
projection document Optional. Specifies the fields to return using
projection operators. Omit this parameter to return
all fields in the matching document.
$OR
The $or operator performs a logical OR operation on an array of two or more
<expressions> and selects the documents that satisfy at least one of the
<expressions>. The $or has the following syntax:
{ $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }
Consider the following example:
db.inventory.find( { $or: [ { quantity: { $lt: 20 } }, { price: 10 } ] } )
This query will select all documents in the inventory collection where either the
quantity field value is less than 20 or the price field value equals 10.
$NOT
Syntax: { field: { $not: { <operator-expression> } } }
$not performs a logical NOT operation on the specified <operator-expression> and
selects the documents that do not match the <operator-expression>. This includes
documents that do not contain the field.
Consider the following query:
db.inventory.find( { price: { $not: { $gt: 1.99 } } } )
This query will select all documents in the inventory collection where:
the price field value is less than or equal to 1.99 or
the price field does not exist
{ $not: { $gt: 1.99 } } is different from the $lte operator. {$lte: 1.99} returns only the
documents where price field exists and its value is less than or equal to 1.99.
Query for Null or Missing Fields
Different query operators in MongoDB treat null values differently.
The examples is given below,
db.users.insert(
[{ "_id" : 900, "name" : null },

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()

MongoDB Update() Method

11 | 6 5
Department of Information Technology, KBTCOE

The update() method updates the values in the existing document.


The basic syntax of update() method is as follows −
>db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)
MongoDB Save() Method
The save() method replaces the existing document with the new document passed in
the save() method.
The basic syntax of MongoDB save() method is shown below −
>db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})
1.6 Conclusion: In this way we can use various mongodb methods to perform
queries.
1.7 Questions
1. What is Mongodb?
2. Explain find method with suitable example?

Signature of Staff with Date

12 | 6 5
Department of Information Technology, KBTCOE

Experiment No: Group_C_03 Date:


1.1 Aim: Execute at least 10 queries on any suitable MongoDB database that
demonstrates following:
$ where queries
Cursors (Limits, skips, sorts, advanced query options)
Database commands
1.2 Objectives: 1. To learn SQL DCL, DDL commands.
2. To Learn ER diagram and its features.
1.3 Hardware used: Computer System
1.4 Softwares used / Programming Languages Used:
Open source operating system (Linux).
MongoDB
1.5. Theory:
Introduction:
Where
We can use the $where operator to pass either a string containing a JavaScript
expression or a full JavaScript function to the query system. The $where provides
greater flexibility, but requires that the database processes the JavaScript expression
or function for each document in the collection. Reference the document in the
JavaScript expression or function using either this or obj .
Examples:
db.myCollection.find( { $where: "this.credits == this.debits" } );
db.myCollection.find( { $where: "obj.credits == obj.debits" } );
db.myCollection.find( { $where: function() { return (this.credits = this.debits) } } );
db.myCollection.find( { $where: function() { return obj.credits == obj.debits; } } );
Cursor
The db.collection.find() method returns a cursor. To access the documents, we need to
iterate the cursor. However, in the mongo shell, if the returned cursor is not assigned
to a variable using the var keyword, then the cursor is automatically iterated up to 20
times to print up to the first 20 documents in the results. In the mongo shell, when we

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:

Parameter Type Description

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

Query and Write Operation Commands


Name Description
Find Selects documents in a collection.
Insert Inserts one or more documents.
Update Updates one or more documents.
Delete Deletes one or more documents.
findAndModify Returns and modifies a single document.
getMore Returns batches of documents currently pointed to by the cursor.
getLastError Returns the success status of the last operation.
getPrevError Returns status document containing all errors since the last
resetError command.
resetError Resets the last error status.
Eval Deprecated. Runs a JavaScript function on the database server.

17 | 6 5
Department of Information Technology, KBTCOE

parallelCollectionScan Lets applications use multiple parallel cursors when


reading documents from a collection.

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?

Signature of Staff with Date

18 | 6 5
Department of Information Technology, KBTCOE

Experiment No: Group_C_04 Date:


1.1 Aim: Implement Map reduce example with suitable example.
1.2 Objectives: 1. To learn the map reduce algorithm.
2. Learn to apply map reduce algorithm for different Applications.
1.3 Hardware used: Computer System
1.4 Softwares used / Programming Languages Used:
Open source operating system (Linux).
MongoDB
1.5. Theory:
Introduction:
As per the MongoDB documentation, Map-reduce is a data processing paradigm for
condensing large volumes of data into useful aggregated results. MongoDB uses map
Reduce command for map-reduce operations. Map Reduce is generally used for
processing large data sets.
MapReduce Command
Following is the syntax of the basic mapReduce command −
>db.collection.mapReduce(
function() {emit(key,value);}, //map function
function(key,values) {return reduceFunction}, { //reduce function
out: collection,
query: document,
sort: document,
limit: number
}
)
The map-reduce function first queries the collection, then maps the result documents
to emit key-value pairs, which is then reduced based on the keys that have multiple
values.
In the above syntax −
• map is a javascript function that maps a value with a key and emits a key-
value pair

19 | 6 5
Department of Information Technology, KBTCOE

• reduce is a javascript function that reduces or groups all the documents


having the same key
• out specifies the location of the map-reduce query result
• query specifies the optional selection criteria for selecting documents
• sort specifies the optional sort criteria
• limit specifies the optional maximum number of documents to be returned
Using MapReduce
Consider the following document structure storing user posts. The document stores
user_name of the user and the status of post.
{
"post_text": "tutorialspoint is an awesome website for tutorials",
"user_name": "mark",
"status":"active"
}
For example, use a map Reduce function on our posts collection to select all the
active posts, group them on the basis of user_name and then count the number of
posts by each user using the following code −
>db.posts.mapReduce(
function() { emit(this.user_id,1); },
function(key, values) {return Array.sum(values)}, {
query:{status:"active"},
out:"post_total"
}
)
The above map Reduce query outputs the following result −
{
"result" : "post_total",
"timeMillis" : 9,
"counts" : {
"input" : 4,
"emit" : 4,
"reduce" : 2,
"output" : 2

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?

Signature of Staff with Date

21 | 6 5
Department of Information Technology, KBTCOE

Experiment No: Group_C_05 Date:


1.1 Aim: Implement the aggregation and indexing with suitable example in
MongoDB. Demonstrate the following:
1. Aggregation framework
2. Create and drop different types of indexes and explain () to show the
advantage of the indexes.
1.2 Objectives: 1. To learn the aggregation and indexing concept in mongodb.
2. To Learn the concept of index in mongodb.
1.3 Hardware used: Computer System
1.4 Softwares used / Programming Languages Used:
Open source operating system (Linux).
MongoDB
1.5. Theory:
Introduction:
Aggregation
Aggregations operations process data records and return computed results.
Aggregation operations group values from multiple documents together, and can
perform a variety of operations on the grouped data to return a single result. In SQL
count(*) and with group by is an equivalent of mongodb aggregation.
The aggregate() Method
For the aggregation in MongoDB, aggregate() method is used.
Syntax
Basic syntax of aggregate() method is as follows −
>db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
Example
In the collection you have the following data −
{
_id: ObjectId(7df78ad8902c)
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by_user: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],

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?

Signature of Staff with Date

26 | 6 5
Department of Information Technology, KBTCOE

Experiment No: Group_D_01 Date:


1.1 Aim: To implement a mini project/sample application.
1.2 Objectives: Study and implement simple commercial application using PHP as
Front End and MongoDB as Back End.
1.3 Hardware used: Computer System
1.4 Softwares used / Programming Languages Used:
Open source operating system (Linux).
MongoDB
1.5 Theory:
Prepare a detailed report of your mini project covering following details:
1. Title of the Project.
2. Abstract
3. Introduction
4. Scope
5. Software/Hardware Requirements Specification
6. Entity Relationship Diagram with EER features
7. Data Dictionary
8. Relational Database Design
9. Database Normalization
10. Graphical User Interface(Screenshots)
11. Source Code: (should be in CD)
12. Data Reports
13. Testing document
14. Future Enhancement
15. Conclusion
16. References/Bibliography

27 | 6 5
Department of Information Technology, KBTCOE

Experiment No: Group_E_01 Date:


1.1 Aim: To create simple objects and array objects using JSON.
1.2 Objectives: Create various objects using JSON and implement the same to
create array objects.
1.3 Hardware used: Computer System
1.4 Softwares used / Programming Languages Used:
Open source operating system (Linux).
MongoDB
1.5 Theory:
JSON (Java Script Object Notation)
JSON stands for JavaScript Object Notation. This format was specified by Douglas
Crockford. This was designed for human-readable data interchange. JSON has been
extended from the JavaScript scripting language.
Uses of JSON
1. JSON is used when writing JavaScript based application which includes browser
extension and websites.
2. JSON format is used for serializing & transmitting structured data over network
connection.
3. JSON is primarily used to transmit data between server and web application.
4. Web Services and API.s use JSON format to provide public data.
5. JSON can be used with modern programming languages.
Characteristics of JSON
• It is easy to read and write JSON.
• JSON is lightweight text based interchange format
• JSON is language independent.

Data Types Supported by JSON


The various data types supported by JSON are shown in following table:
Table 12.1 Data Types supported by JSON
Creation of Simple Objects using JSON
JSON objects can be created with Java Script.
To create an empty object:
var JSONObj = {};

28 | 6 5
Department of Information Technology, KBTCOE

To create a new object:


var JSONObj = new Object();
Object
It is an unordered set of name/value pairs. Object are enclosed in curly braces that is it
starts with '{' and ends with '}'. Each name is followed by ':'(colon) and the
name/value pairs are separated by , (comma).
The keys must be strings and should be different from each other. Objects should be
used when the key names are arbitrary strings.
Syntax:
{key: value, .......}
Example showing Object:
{“id": "011A", "language": "JAVA", "price": 500, }
Example:
<html>
<head>
<title>Welcome to VPCOE TO LEARN JSON </title>
<script language="javascript" >
var JSONObj = { "name" : "www.vpcoe.org", "academic_year": 2014 };
document.write("<h1>JSON with JavaScript example</h1>");
document.write("<br>");
document.write("<h3>Website Name="+JSONObj.name+"</h3>");
document.write("<h3>Academic Year="+JSONObj.year+"</h3>");
</script>
</head>
<body>
</body>
</html>
Write the above code in any editor and save with extension .html. Output can be
viewed through any web browser by opening this html file in web browser.
Creation of an Array Object in JSON
Array objects are the objects containing multiple objects in sequential fashion which
are separated by comma (,). We can maintain various objects inside a single array
object,

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?

Signature of staff with date

31 | 6 5
Department of Information Technology, KBTCOE

Experiment No: Group_E_02 Date:


1.1 Aim: To encode and decode JSON objects using PHP/JAVA.
1.2 Objectives: Study and demonstrate the use of encoding and decoding JSON
objects using PHP/JAVA.
1.3 Hardware used: Computer System
1.4 Softwares used / Programming Languages Used:
Open source operating system (Linux).
MongoDB
1.5 Theory:
JSON extension is bundled with PHP by default from version 5.2.0 so there is no need
of any special environment.
JSON Functions:
1. json_encode: It returns the JSON representation of a value.
2. json_decode: It decodes a JSON string.
3. json_last_error: It returns the last error occurred.
Encoding:
json_encode () function is used for encoding which returns JSON representation of a
value.
Syntax:
string json_encode ( $value [, $options = 0 ] )
The value parameter specifies value being specified. It works only with UTF-8
encoded data. The options parameter specifies the a bitmask consisting of
JSON_HEX_QUOT,JSON_HEX_TAG,JSON_HEX_AMP,JSON_HEX_APOS,JSO
N_NUMERIC_CHECK,JSON_PRETTY_PRINT,JSON_UNESCAPED_SLASHES,
JSON_FORCE_OBJECT.
Example:
The following PHP code
<?php
class Emp {
public $name = "";
public $hobbies = "";
public $birthdate = "";
}

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.

Signature of staff with date

34 | 6 5

You might also like