Mongodb and Neo4j Practicals
Mongodb and Neo4j Practicals
Explanation : we can crete the node with keyword CREATE. here ajay is a node
name with label person and having the properties like name,place, project and
age and i am going to return node ajay with RETURN keyword.
like wise i have created some other nodes
Like wise we are going to create other nodes with label place
Q2) Model the following Dairy Brand information as a graph model , and
answer the following queries using Cypher. There are various dairy brands
like Amul, Go, Britannia, Gokul etc. Their popularity varies across
different states in India. The popularity is measured as %, with a high
popularity defined as >=90%, Medium Popularity between 50 to 90%, and
Low popularity <50%. Each brand manufactures various types of Dairy
products like milk, butter, cheese, Curd etc. The milk product can be
categorized into Low fat/medium fat or high fat content type.
1. Identify the labels and relationships, along with their properties, and
draw a high level Graph model.
2. Create nodes and relationships, along with their properties, and
visualize your actual Graph model.
3. Answer the following queries using Cypher:
a. List the names of different brands considered in your graph.
b. List the brands that are highly popular in Maharastra.
c. List the popular cheese brands in Gujarat
d. List the brands manufacturing “low” fat content milk
solution:
Creation of Graph Model
>Create(mamul:dairy{brand:"amul",pop_perc:96,state:"maharashtra"}),(mgo:da
iry{brand:"go",pop_perc:80,state:"maharashtra"}),(mbritania:dairy{brand:"brita
nia",pop_perc:55,state:"maharashtra"}),(mgokul:dairy{brand:"gokul",pop_perc:
96,state:"maharashtra"}),(gamul:dairy{brand:"amul",pop_perc:98,state:"gujrat"
}),(ggo:dairy{brand:"go",pop_perc:80,state:"gujrat"}),(gbritania:dairy{brand:"b
ritania",pop_perc:55,state:"gujrat"}),(ggokul:dairy{brand:"gokul",pop_perc:56,
state:"gujrat"})
Create milk product
>Create(p1_milk:product{pname:"milk",con_type:"LF"}),(p2_milk:product{pn
ame:"milk",con_type:"MF"}),(p3_milk:product{pname:"milk",con_type:"HF"}
)
Create cheese product
>CREATE(p1_cheese:product{pname:"cheese",con_type:"LF"}),(p2_cheese:pr
oduct{pname:"cheese",con_type:"MF"}),(p3_cheese:product{pname:"cheese",c
on_type:"HF"})
Create brands label them dairyC
>create(amul:dairy{brand:"amul",Syear:1960}),(go:dairy{brand:"go",Syear:197
0}),(britania:dairy{brand:"britania",Syear:1980}),(gokul:dairy{brand:"gokul",S
year:1990})
Create states
>CREATE (maha:state{name:"maharashtra"}),(guj:state{name:"gujrat"})
Create relation between dairy and state
>match (amul:dairy),(maha:state) where amul.brand="amul" and maha.name=
"maharashtra" create (amul)-[r1:SALES_IN{pop_perc:85}]-> (maha)
>match (amul:dairy),(guj:state) where amul.brand="amul" and guj.name=
"gujrat" create (amul)-[r1:SALES_IN{pop_perc:95}]-> (guj)
>match (go:dairy),(maha:state) where go.brand="go" and maha.name=
"maharashtra" create (go)-[r1:SALES_IN{pop_perc:45}]-> (maha)
>match (go:dairy),(guj:state) where go.brand="go" and guj.name= "gujrat"
create (go)-[r1:SALES_IN{pop_perc:75}]-> (guj)
>match (britania:dairy),(maha:state) where britania.brand="britania" and
maha.name= "maharashtra" create (britania)-[r1:SALES_IN{pop_perc:65}]->
(maha)
>match (gokul:dairy),(maha:state) where gokul.brand="gokul" and maha.name=
"maharashtra" create (gokul)-[r1:SALES_IN{pop_perc:65}]-> (maha)
Create milk Products
>CREATE(p1_milk:product:milk{name:"LFmilk",con_type:"LF"}),(p2_milk:p
roduct:milk{name:"MFmilk",con_type:"MF"}),(p3_milk:product:milk{name:"
HFmilk",con_type:"HF"})
>CREATE (p_cheese:product:cheese{name:"cheese"})
>CREATE (p_butter:product:butter{name:"butter"})
create relation between dairy and products
>match (a:dairy),(b:milk) where a.brand="amul" and b.name= "LFmilk" create
(a)-[r1:MANUFACTURE]-> (b)
>match (a:dairy),(b:milk) where a.brand="amul" and b.name= "MFmilk" create
(a)-[r1:MANUFACTURE]-> (b)
>match (a:dairy),(b:milk) where a.brand="amul" and b.name= "HFmilk" create
(a)-[r1:MANUFACTURE]-> (b)
>match (a:dairy),(b:milk) where a.brand="go" and b.name= "LFmilk" create
(a)-[r1:MANUFACTURE]-> (b)
>match (a:dairy),(b:milk) where a.brand="britania" and b.name= "LFmilk"
create (a)-[r1:MANUFACTURE]-> (b)
>match (a:dairy),(b:cheese) where a.brand="amul" and b.name= "cheese" create
(a)-[r1:MANUFACTURE]-> (b)
>match (a:dairy),(b:butter) where a.brand="go" and b.name= "butter" create (a)-
[r1:MANUFACTURE]-> (b)
a) List the names of different brands considered in your graph.
>match(n:dairy) return n.brand
b)List the brands that are highly popular in Maharashtra
>match (a:dairy)-[r1:SALES_IN]-> (b:state)
WHERE r1.pop_perc>=70 and b.name="maharashtra"
RETURN a.brand
c)List the popular cheese brands I Gujrath
>MATCH (a:dairy)-[r1:SALES_IN]-> (b:state) , (a:dairy)-
[r2:MANUFACTURE]-> (c:cheese)
WHERE b.name=“gujrat"
RETURN a.brand
d)List the brands manufacturing “low” and Fact content milk
>MATCH (a:dairy)-[r1:MANUFACTURE]-> (b:milk) WHERE
b.name="LFmilk"
RETURN a.brand
2)> db.collegeee.find({professor:{$elemMatch:{age:{$gt:40}}}})
{ "_id" : 1, "collegename" : "sarhad", "course" : [ "arts", "science", "comp" ],
"professor" : [ { "name" : "abc", "age" : 45 }, { "name" : "pqr", "age" : 30 } ] }
{ "_id" : 2, "collegename" : "abhinav", "course" : [ "arts", "science", "comp" ],
"professor" : [ { "name" : "abc", "age" : 45 }, { "name" : "qr", "age" : 30 } ] }
{ "_id" : 3, "collegename" : "sp", "course" : [ "science", "comp" ], "professor" :
[ { "name" : "bc", "age" : 45 }, { "name" : "qr", "age" : 30 } ] }
{ "_id" : 4, "collegename" : "dsp", "course" : [ "science", "comp" ], "professor" :
[ { "name" : "abc", "age" : 45 }, { "name" : "qr", "age" : 30 } ] }
{ "_id" : 5, "collegename" : "dspe", "course" : [ "arts", "science", "comp" ],
"professor" : [ { "name" : "aabc", "age" : 45 }, { "name" : "wqr", "age" : 50 } ] }
1)> db.collegeee.find({course:"arts"}).pretty()
{
"_id" : 1,
"collegename" : "sarhad",
"course" : [
"arts",
"science",
"comp"
],
"professor" : [
{
"name" : "abc",
"age" : 45
},
{
"name" : "pqr",
"age" : 30
}
]
}
{
"_id" : 2,
"collegename" : "abhinav",
"course" : [
"arts",
"science",
"comp"
],
"professor" : [
{
"name" : "abc",
"age" : 45
},
{
"name" : "qr",
"age" : 30
}
]
}
{
"_id" : 5,
"collegename" : "dspe",
"course" : [
"arts",
"science",
"comp"
],
"professor" : [
{
"name" : "aabc",
"age" : 45
},
{
"name" : "wqr",
"age" : 50 }]}
Q4) Model the following Hospitals information as a graph model, and
answer the following queries using Cypher. Consider hospitals in and
around Pune. Each hospital may have one or more specializations like
Pediatric, Gynaec, Orthopaedic, etc. A person can recommend/provide
review for a hospital. A doctor can be associated with one or more
hospitals. 1. Identify the labels and relationships, along with their
properties, and draw a high level Graph model.
2. Create nodes and relationships, along with their properties, and visualize
your actual Graph model.
3. Answer the Queries
a. List the names of hospitals with pediatric specialization.
b. List the Names of doctors who are visiting “Jehangir Hospital ” on
Mondays.
c. List the most recommended Hospital for Gynaec specialization.
d. List the names of people who have given a rating of (>=3) for “Jehangir
Hospital”[5]
Solution
1)> db.Hospital.insert({Hno:1,Hname:"AAA",Specialization:["Pedi
atric","Gynaec","Orthopaedic"],People:[{Pname:"PQR",Ratin
g:4},{Pname:"SDE",Rating:5}],Doctor:[{"Dname" : "WWW", "Visit" :
"Sunday"}]}) WriteResult({ "nInserted" : 1 })
2)> db.Hospital.insert({Hno:2,Hname:"BBB",Specialization:["Gyna
ec","Orthopaedic"],People:[{Pname:"POP",Rating:2},{Pname:
"SDE",Rating:3}],Doctor:[{"Dname":"XXX",Visit:"Monday"}]})
WriteResult({ "nInserted" : 1 })
3)> db.Hospital.insert({Hno:3,Hname:"CCC",Specialization:["Gyna
ec","Orthopaedic","Pediatric"],People:[{Pname:"KLO",Rating:3},{Pname:"LP
O",Rating:3}],Doctor:[{"Dname" : "XXX","Visit": "Tuesday"}]})
WriteResult({ "nInserted" : 1 })
Solution:
> db.product.insert({name:"robot",price:12000})
WriteResult({ "nInserted" : 1 })
> db.product.insert({name:"toycar",price:2000})
WriteResult({ "nInserted" : 1 })
> db.product.insert({name:"cricketset",price:9000})
WriteResult({ "nInserted" : 1 })
> db.product.insert({name:"studymaterial",price:19000})
WriteResult({ "nInserted" : 1 })
>db.order.insert({orderno:3736,custName:"arunkumar",product:{productName:
"toycar",price:20000},order_date:"12/2/2019",stetus:"processed",Totalbill:2039
,invoice:{invoiceNO:67564,bill:2039,date:"17/2/2019"}})
WriteResult({ "nInserted" : 1 })
>db.order.insert({orderno:3737,custName:"arunkumar",product:{productName:
"robot",price:12000},order_date:"11/3/2019",stetus:"processed",Totalbill:12800
,invoice:{invoiceNO:67574,bill:12039,date:"17/3/2019"}})
WriteResult({ "nInserted" : 1 })
>db.order.insert({orderno:3738,custName:"arunkumar",product:{productName:
"cricketset",price:9000},order _date:"15/5/2019",stetus:"in
process",Totalbill:9050})
WriteResult({ "nInserted" : 1 })
>db.order.insert({orderno:3739,custName:"mukeshpatil",product:{productNam
e:"studentmaterial",price:19000} ,order_date:"15/8/2019",stetus:"in
process",Totalbill:19080}) WriteResult({ "nInserted" : 1 })
4)
a)> db.product.find().pretty()
b) > db.order.find({Totalbill:{$lt:10000}})
c) > db.order.find({stetus:"in process"})
d) >db.order.find({custName:"arun kumar",stetus:"processed"})
>db.tour.insert({sourc:"john",destination:"taj",toerisumName:"rohit",tourisumra
te:60090,expense:10400,year:2016,custo
mer:[{cname:"mukesh",city:"pune"},{cname:"abhijeet
sangita",city:"baramati"},{cname:"manisha",city:"15no"},{cna
me:"manasi",city:"latur"}]})
4)
a) >db.turisum.find({name:"veena word"}).pretty()
b) >db.turisum.find({}).sort({"rate":-1}).limit(1)
c)>db.tour.aggregate([{"$sort":{"year":1}},{"$limit":3},{$group:{_id:null,"cou
nt":{"$sum":"$expense"}}}])
d) > db.tour.find({destination:"shillong"})