Full Final
Full Final
IT(SEM - V)
INDEX
To create database , open the Mongodb app then connect to the localhost server then display
board of Mongodb will appear then click on create database > then add database name and
collection name and click on create database.
Drop database : To Delete a database that you don’t want anymore, simply type :
use <db_name>
db.dropDatabase()
2. findOne()
db.student.findOne()
(The output comes after inputting the collections and inserting them)
Drop Collection
Dev Hitesh Bhatt 103 EMERGING TECHNOLOGIES
MALINI KISHOR SANGHVI COLLEGE OF COMMERCE TY BSC.IT(SEM - V)
To delete a collection, simply use:
db.collection.drop()
C. Question: Write a Mongodb Query to insert, query, update and delete. Create
using insert()
There are two ways to insert documents inside a collection using insert():
I. Writing the data directly inside insert().
use mks
db.student.insert({"name":”nirmeet dave","roll":108})
Update a document
To update a document,
db.student.update({"name":"nirmeet dave"}, {"name":"jemin dave"})
Delete a document
To delete a document simply use remove() method
db.student.remove({"name":"nirmeet dave"})
QUERY:
db.products.aggregate([ {$match:{}}, { $group:{ _id:"$Customer", Product:
{$first:"$Product"}, total_items:{$sum:"$Total"} } } ])
This will output the different products taken by different customers and their total.
OUTPUT:
$min:
This is used to get the minimum integer value present in the column. In this example, we
shall see the minimum no. of items that a customer has taken.
QUERY:
db.products.aggregate( [ {$match:{}}, {$group:{_id:"$Customer",product:
{$first:"$Product"},total_items:{$min:"$total"} }} ] )
OUTPUT:
QUERY:
db.products.aggregate( [ {$match:{}}, {$group:{_id:"$Customer",product:
{$first:"$Product"},total_items:{$max:"$total"} }} ] )
OUTPUT:
$avg:
This expression is used to find the average of the integer values in a specified column. In this
example, we shall find the average of the total items a customer has taken.
QUERY:
db.products.aggregate( [ {$match:{}}, {$group:{_id:"$Customer",product:
{$first:"$Product"},total_items:{$avg:"$total"} }} ] )
OUTPUT:
$push:
The $push operator appends a specified value to an array.
The $push operator has the form : {$push: { <field1>:<value1>, ... } }
To specify a in an embedded document or in an array, use dot notation.
In this example lets say we have a DB as PushDB,
And the collection name is test.
So, use the following queries one by one.
use PushDB
db.createCollection(‘test’)
And then, insert this document:
db.test.insert({'_id':1,'name':'samiksha', 'scores':[80]})
QUERY:
db.test.update( { Customer: "ABC" }, { $push: { Total: 89 } } )
OUTPUT:
$addToSet:
Dev Hitesh Bhatt 103 EMERGING TECHNOLOGIES
MALINI KISHOR SANGHVI COLLEGE OF COMMERCE TY BSC.IT(SEM - V)
The $addToSet operator adds a value to an array unless the value is already present, in which
case $addToSet does nothing to that array.
The $addToSet operator has the form : { $addToSet: :{ <field1>:<value1>, ...}}
To specify a <field> in an embedded document or in an array, use dot notation.
QUERY:
db.test.update( { _id: 1 }, { $addToSet: { surname: "tendulkar" } } )
This will add the surname field in the document and set it to tendulkar.
OUTPUT:
QUERY:
db.products.aggregate( [ { $sort: { Customer:1 } }, { $group: { _id: "$Product",
firstItemTotal: { $first: "$Total" } } } ] )
OUTPUT:
$last:
Returns the value that results from applying an expression to the last document in a group of
documents that share the same group by a field. Only meaningful when documents are in a
defined order.
$last is only available in the $group stage.
QUERY:
db.products.aggregate( [ { $sort: { Customer:1 } }, { $group: { _id: "$Product",
lastItemTotal: { $last: "$Total" } } } ] )
OUTPUT:
$eq
Matches values that are equal to a specified value
{Key: {$eq: value}}
> db.Student.find({ "Age": {"$eq": 19}})
(To find students whose age is equal to 19)
OUTPUT:
$gte :
Matches values that are greater than or equal to a specified value
{Key ($gte value}}
> db.Student.find({ "Age": { "$gte": 20}})
(To find students whose age is greater than equal to 20)
OUTPUT:
$lt :
Matches values that are less than a specified value.
{Key: {$lt: value}}
> db.Student.find({ "Age": { "$lt" : 20}})
(To find students whose age is less than 20)
OUTPUT:
$lte :
Dev Hitesh Bhatt 103 EMERGING TECHNOLOGIES
MALINI KISHOR SANGHVI COLLEGE OF COMMERCE TY BSC.IT(SEM - V)
Matches values that are less than or equal to a specified value.
{Key {$lte value}}
> db.Student.find({ "Age": { "$1te" 19}})
(To find students whose age is less than equal to 19)
OUTPUT:
$ne :
Matches all values that are not equal to a specified value
{Key {$ne: value}}
> db.Student.find({ "Age": { "$ne" 19}})
(To find students whose age is not equal to 19)
OUTPUT:
$in :
Matches any of the values specified in an array.
{Key: {$in: value}}
>db.student.find({"Class":{"$in":["C1","c2"]}})
(To find students who belong to either class C1 or C2)
OUTPUT:
OUTPUT:
OUTPUT:
OUTPUT:
AFTER CLICKING ON THE BUTTON “Click this button to hide the paragraphs.” the
paragraphs will disappear as shown below in the paragraph.
OUTPUT:
OUTPUT:
OUTPUT:
OUTPUT:
In the above image, it meant to show that the colour of the text got changed to red, and the
text would slide up first and then slide down.
CODE:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
alert("Text: " + $("#myh2").text());
});
$("#btn2").click(function(){
alert("HTML: " + $("#myh2").html());
});
});
</script>
</head>
<body>
<h2 id="myh2">This is h2 with <i>italic,</i><b>bold</b>, and <u>underline</u>
fonts.</h2>
<button id="btn1">Show Text</button>
<button id="btn2">Show HTML</button>
</body>
OUTPUT:
CODE:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("#test1").text($("#test3").val());
});
$("#btn2").click(function(){
$("#test2").html($("#test3").val());
});
$("#btn3").click(function(){
$("#test3").val("Value changed.");
});
});
</script>
</head>
<body>
<p id="test1">change my text.</p>
Dev Hitesh Bhatt 103 EMERGING TECHNOLOGIES
MALINI KISHOR SANGHVI COLLEGE OF COMMERCE TY BSC.IT(SEM - V)
<p id="test2">change my html.</p>
<p>Change my value: <input type="text" id="test3" value="Mickey Mouse"></p>
<button id="btn1">Set Text</button>
<button id="btn2">Set HTML</button>
<button id="btn3">Set Value</button>
</body>
</html>
OUTPUT:
Before clicking any button :
CODE:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").remove();
});
});
</script>
</head>
<body>
<div id="div1">
<h5>I will be removed if you click the button.</h5>
</div>
<br>
<button>Remove above element</button>
</body>
</html>
OUTPUT:
PRACRTICAL NO 10 (D):
QUESTION: jQuery empty() Method :
The jQuery empty() method removes the child elements of the selected element(s).
PRACTICAL 11:
QUESTION: (A): Creating JSON
There are many ways to create JSON with different programming languages like,
Java, Python and PHP. We shall see how to create a JSON in Python.
Python has a built-in package called JSON, which can be used to work with JSON
data.
Import the JSON module:
import json
To create a JSON in python, we need to create a python object.
CODE: import json
# a Python object (dictionary):
x={
"name": "dev",
"age": 19,
"city": "Mumbai"
}
# convert into JSON:
y = json.dumps(x)
# the result is a JSON string:
print(y)
OUTPUT:
● Save it as persist.json and open a command prompt with its current directory set to
Dev Hitesh Bhatt 103 EMERGING TECHNOLOGIES
MALINI KISHOR SANGHVI COLLEGE OF COMMERCE TY BSC.IT(SEM - V)
Mongodb’s bin path. (C:/Program Files/Mongodb/Server/4.0/bin in my case).