NGT Practical
NGT Practical
Creating database
Drop Database
db.createCollection("diz",{capped:true,autoIndexId:true,size:641232,max:200})
Display Collection
show collections
Drop Collection
db.aditya.drop()
COMMAND:
db.student.insert({"name":"aditya","rollno":4})
db.aditya.insertOne({"name":"aditya","rollno":34,"age":19})
db.aditya.insertMany([{"name":"aditya","rollno":34,"age":19},{"name":"aditya34","roll
no":74,"age":9}])
1)
PRACTICAL 2
Simple Queries with MongoDB
MONGOIMPORT COMMAND
mongoimport --host localhost --db aditya --collection aditya --type json --file "c:\restaurants
(1).json"
1.
3.
2.
5
6.
7.
8.
9.
10.
11.
13
14.
15.
16
17.
18.
19.
Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those
restaurants which are not belonging to the borough Staten Island or Queens or Bronxor
Brooklyn.
Query:
OUTPUT:
20.
a MongoDB query to find the restaurant Id, name, borough and cuisine for those restaurants
which achieved a score which is not more than 10.Write
Query:
db.rest1.find({"grades.score":{$lte:10}},{"restaurant_id":1,"cuisine":1,"name":1,"borough":1})
OUTPUT:
21.
Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those
restaurants which prepared dish except 'American' and 'Chinees' or restaurant's name
begins with letter 'Wil'
Query:
> db.rest1.find( {$or: [ {name: /^Wil/}, {"$and": [ {"cuisine" : {$ne :"American "}},
{"cuisine" : {$ne :"Chinees"}} ]} ]} ,{"restaurant_id" : 1,"name":1,"borough":1,"cuisine" :1} );
OUTPUT:
22.
Write a MongoDB query to find the restaurant Id, name, and grades for those restaurants
which achieved a grade of "A" and scored 11 on an ISODate "2014-08-11T00:00:00Z"
among many of survey dates.
Query:
db.rest1.find( { "grades.date": ISODate("2014-08-11T00:00:00Z"),
"grades.grade":"A" , "grades.score" : 11 }, {"restaurant_id" :
1,"name":1,"grades":1} );
OUTPUT:
23.
PRACTICAL 4
Bin folder
After going into the bin folder we firstly instantiate 3 mongod replicated servers.
COMMAND:
C:\Program Files\MongoDB\Server\4.0\bin>start mongod --logpath \data\db1\1.log --dbpath
\data\db1 --port 27020 --smallfiles --oplogSize 64 --replSet MyRep
In the rest of terminals only the port will change and as we are making them as secondaries
so we have made them as slaves.
For this COMMAND: rs.slaveOk()
rs.secondaryOk()
After that once all the above process is done you will be able see written as primary in first
server if not press enter onto it
After once all is done it means you have successfully created a replica set.
1. Write a MongoDB query to use sum, avg, min and max expression
CODE:
OPERATIONS
> db.mycollection.aggregate([{$group:{_id:"$by_user",num_likes:{$sum:"$likes"}}}])
{ "_id" : null, "num_likes" : 128 }
> db.mycollection.aggregate([{$group:{_id:"$by_user",num_likes:{$avg:"$likes"}}}])
{ "_id" : null, "num_likes" : 25.6 }
> db.mycollection.aggregate([{$group:{_id:"$by_user",num_likes:{$min:"$likes"}}}])
{ "_id" : null, "num_likes" : 13 }
> db.mycollection.aggregate([{$group:{_id:"$by_user",num_likes:{$max:"$likes"}}}])
{ "_id" : null, "num_likes" : 34 }
.
2. Write a MongoDB query to use push and addToSet expression
CODE:
> db.mycollection.aggregate([{$group:{_id:"$by_user",asm:{$push:"$url"}}}])
{ "_id" : null, "asm" : [ "www.practicalmongodb.com", "www.practicalmongodb.com",
"www.wof.com", "www.as.com", "www.awp.com", "www.asm34.com" ] }
> db.mycollection.aggregate([{$group:{_id:"$by_user",as34:{$addToSet:"$url"}}}])
{ "_id" : null, "as34" : [ "www.asm34.com", "www.wof.com", "www.practicalmongodb.com",
"www.awp.com", "www.as.com" ] }
PRACTICAL 5
PROCEDURE:
NGT PRACTICAL 5
Practical 5 mongodb and Java
Jar file for that
Java program to communicate to a database
We require a driver fil
Driver required -Mongo Java driver 3.12.14 = download from internet
1.environmental.variables
User variables
System variables-we edit
Path variables-files we need for our application
Cick on path- edit - (no have settled)-new- paste the path(go to bin)-ok our path set
Set the class path - create a new var- var name: class path, value: ".;(where path of har file
along with name);"-ok
Setting is in placed
JAVA PROGRAM
4 PROGRAMS - 40 marks
Explaination
Creating the data
Import statement
4 lines com.mongodb.client - packages available on jar file
Classes- mongocollection,mongodatabase,mongoclient,mongocredential
Org.bson.document- org.bson packages
EXECUTION PART
BIN FOLDER>mongod
Mongo
Another cmd in administrator mode
Move to location of Java program
Type>javac insertingdoc.java
Type>java Inserting document
In mongo
>Show dbs
>use mydb6
>Show Collections
Go to collection and find() execute
CODE:
hasNext()- till next value is available- either true or false
UPDATION IN OUR COLLECTION
com.mongodb.DB
DBCollection,DBObject,WriteResult(able to update or not)
DB db = mongo.getDB(If not available then it will not create the database)
query- which data should be updated
Update.put = make changes
DELETION COLLN
Com.
Filters- which collection needed to be updated
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import org.bson.Document;
MongoCollection<Document> collection=
database.getCollection("sampleCollection"); // Accessing the collection
collection.insertOne(document);
jQuery practical:
<!doctype html>
<html>
<head>
<title>Online jQuery Editor</title>
<script src="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script>
<script>
$(document).ready(function() {
$("p").css("background-color", "yellow");
$("button").click(function(){document.write("Hello World")});
$(".class1").css("background-color", "red");
$("#id1").css("background-color", "blue");
$("h1").css("background-color", "purple");
$(".c2").css("background-color", "red");
});
</script>
</head>
<body>
<h1>jQuery element Selector</h1>
<p class="class1">Using of class attribute</p>
<p id="id1">This is p tag</p>
<button class="c2">Click Here</button>
</body>
</html>
<!doctype html>
<html>
<head>
<title>Online jQuery Editor</title>
<script src="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script>
<script>
$(document).ready(function() {
$("p").css("background-color", "yellow");
$("button").click(function(){document.write("Hello World")});
$(".class1").css("background-color", "red");
$("#id1").css("background-color", "blue");
$("h1").css("background-color", "purple");
});
</script>
</head>
<body>
<h1>jQuery element Selector</h1>
<p class="class1">Using of class attribute</p>
<p id="id1">This is p tag</p>
<button class="b1">hover</button>
<p> Hello Students</p>
<p>TyIT</p>
<button id="b2">off</button>
<p id="t1">hello</p>
</body>
</html>