6. Tutorial_MongoDB Query Language (MQL)

pdf

School

University of Calgary *

*We aren’t endorsed by this school

Course

571

Subject

Information Systems

Date

Feb 20, 2024

Type

pdf

Pages

30

Uploaded by jadehaofanzhao

Report
CPSC 571 Design and Implementation of Database Systems CPSC 671 Database Management Systems Winter 2023 Tutorial 06 - 30 March 2023
CONTENTS Examples on JSON Objects MongoDB Query Language (MQL)
Introduction to JSON General Rules: Data is in the name/value pairs A name/value pair consists od a field name followed by a colon, followed by a value, Example: “name”:”Alex” Data is separated by commas Example: “name”: “Alex”, “affiliation”: “University of Calgary” Curly braces hold objects Example: {“name”: “Alex”, “affiliation”: “University of Calgary”} An array is stored in brackets [] Example: [ {“name”: “Alex”, “affiliation”: “University of Calgary”}, {“name”: “Bob”, “affiliation”: “University of Toronto”} ]
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Consider the following database. ISBN is the key of Books and SIN is the key of Readers. Further, ReadsISBN is a foreign key in Readers. Same domain shows books which have the same domain by linking their ISBN numbers. Readers SIN Name Gender ReadsISBN 612304568 Adnan Male 1234087443 682908123 Steve Male 1256813078 653511678 Jackie Female 1279538713 624115233 Nancy Female 1302264348 694718788 Tina Female 1324989983 665322343 Lama Female 1256813078 635925898 Steiman Male 1324989983 SameDomain ISBN1 ISBN2 1234087443 1256813078 1234087443 1279538713 1256813078 1279538713 Books ISBN Title FirstAuthor SecondAuthor Publisher YearPublished 1234087443 Data Processing Alex Mark ACM 2003 1256813078 Big Data Analysis Ken Lama IEEE 2013 1279538713 Data Management Mary Nick ACM 2011 1302264348 Data Collection Josh Tamer Springer 1985 1324989983 Computer Architecture Ali Steiman Springer 2013 1347715618 Financial Analysis Adams Mike ACM 2015 1370441253 Systems Biology Adnan Jalal IEEE 1999
JSON Objects Write the JSON objects which correspond to the rows of these tables. You should have at least 2 objects in nested format and at least 2 objects in flat format. Books ISBN Title FirstAuthor SecondAuthor Publisher YearPublished 1234087443 Data Processing Alex Mark ACM 2003 1256813078 Big Data Analysis Ken Lama IEEE 2013 1279538713 Data Management Mary Nick ACM 2011 1302264348 Data Collection Josh Tamer Springer 1985 1324989983 Computer Architecture Ali Steiman Springer 2013 1347715618 Financial Analysis Adams Mike ACM 2015 1370441253 Systems Biology Adnan Jalal IEEE 1999 Books = [ { “ISBN”: 1234087443, “Title”: “Data Processing”, FirstAuthor ”: “Alex”, SecondAuthor ”: “Mark”, “Publisher”: “ACM”, YearPublished ”: “2003” }, { “ISBN”: 1256813078, “Title”: “ Big Data Analysis ”, FirstAuthor ”: “Ken”, SecondAuthor ”: “Lama”, “Publisher”: “IEEE”, YearPublished ”: “2013” }, …… ]
Readers = [ { “SIN” : “ 612304568 ”, “Name” : “Adnan”, “Gender” : “Male”, ReadsISBN ”: “1234087443” }, { ….. } ] Readers SIN Name Gender ReadsISBN 612304568 Adnan Male 1234087443 682908123 Steve Male 1256813078 653511678 Jackie Female 1279538713 624115233 Nancy Female 1302264348 694718788 Tina Female 1324989983 665322343 Lama Female 1256813078 635925898 Steiman Male 1324989983 “Linking by Keys/Referenced” Technique
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Readers = [ { “SIN” : “ 612304568 ”, “Name” : “Adnan”, “Gender” : “Male”, ReadsISBN: { “Title”: “Data Processing”, FirstAuthor ”: “Alex”, SecondAuthor ”: “Mark”, “Publisher”: “ACM”, YearPublished ”: “2003” } }, { ….. } ] Readers SIN Name Gender ReadsISBN 612304568 Adnan Male 1234087443 682908123 Steve Male 1256813078 653511678 Jackie Female 1279538713 624115233 Nancy Female 1302264348 694718788 Tina Female 1324989983 665322343 Lama Female 1256813078 635925898 Steiman Male 1324989983 “Embedded/Nested” Technique
SameDomain = [ { “ISBN1”: “ 1234087443 ”, “ISBN2”: “ 1256813078 }, { ….. } ] SameDomain ISBN1 ISBN2 1234087443 1256813078 1234087443 1279538713 1256813078 1279538713 “Linking by Keys/Referenced” Technique
“Embedded/Nested” Technique SameDomain ISBN1 ISBN2 1234087443 1256813078 1234087443 1279538713 1256813078 1279538713 SameDomain = [ { “ISBN1”: { “ISBN”: 1234087443, “Title”: “Data Processing”, FirstAuthor ”: “Alex”, SecondAuthor ”: “Mark”, “Publisher”: “ACM”, YearPublished ”: “2003” }, “ISBN2”: { “ISBN”: 1256813078, “Title”: “ Big Data Analysis ”, FirstAuthor ”: “Ken”, SecondAuthor ”: “Lama”, “Publisher”: “IEEE”, YearPublished ”: “2013” }, ……. ]
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
MongoDB Query Language (MQL)
Assume you have a Suppliers database stored in mongoDB. Solve the following queries using MQL 1. Write a MongoDB query to display all the documents in the collection Suppliers. Solution: db.Suppliers.find();
2. Write a MongoDB query to display the fields Supplier_id , name , Location and Capacity for all the documents in the collection Supplier. Solution: db.Suppliers.find({},{"Supplier_id " :1, "name":1,"Location":1,”Capacity” :1}); 3. Write a MongoDB query to display the fields Supplier_id , name , Location and cuisine , but exclude the field _id for all the documents in the collection Supplier. Solution: db.Suppliers.find({},{"Supplier_id" : 1, "name":1, "Location":1, ”Capacity” :1, "_id":0});
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
4. Write a MongoDB query to display the fields Supplier_id , name , Location and zip code , but exclude the field _id for all the documents in the collection Supplier. Solution: db.Suppliers.find({},{"Supplier_id" : 1, "name":1, "Location":1, "address.zipcode":1, "_id":0}); 5. Write a MongoDB query to display all the Supplier which is in the Location Calgary . Solution: db.Suppliers.find({"Location": "Calgary"});
6. Write a MongoDB query to display the first 5 Suppliers which is in the Location Calgary . Solution: db.Suppliers.find({"Location": "Calgary"}).limit(5); 7.Write a MongoDB query to display the next 5 Suppliers after skipping first 5 which are in the Location Calgary . Solution: db.Suppliers.find({"Location": "Calgary"}).skip(5).limit(5);
8. Write a MongoDB query to find the Suppliers who achieved a score more than 90 . Solution: db.Suppliers.find({grades : { $elemMatch:{"score":{$gt : 90}}}}); 9. Write a MongoDB query to find the Suppliers that achieved a score , more than 80 but less than 100 . Solution: db.Suppliers.find({grades : { $elemMatch:{"score":{$gt : 80 , $lt :100}}}});
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
10. Write a MongoDB query to find the Suppliers which locate in latitude value less than -95.754168 . Solution: db.Suppliers.find({"address.coord" : {$lt : -95.754168}}); 11. Write a MongoDB query to find the Suppliers that do not prepare any Capacity of 'Water tank' and their grade score more than 70 and latitude less than -65.754168 . Solution: db.Suppliers.find( {$and: [ {“Capacity” : {$ne :"Water tank "}}, {"grades.score" : {$gt : 70}}, {"address.coord" : {$lt : -65.754168}} ] } );
12. Write a MongoDB query to find the Suppliers which do not prepare any Capacity of 'Water tank' and achieved a score more than 70 and located in the longitude less than -65.754168 . Note : Do this query without using $and operator. Solution: db.Suppliers.find( { “Capacity” : {$ne : "Water tank "}, "grades.score" :{$gt: 70}, "address.coord" : {$lt : -65.754168} } ); 13. Write a MongoDB query to find the Suppliers which do not prepare any Capacity of 'Water tank ' and achieved a grade point 'A ' not belongs to the Location Edmonton . The document must be displayed according to the cuisine in descending order . Solution: db.Suppliers.find( { “Capacity” : {$ne : "Water tank "}, "grades.grade" :"A", "Location": "Edmonton" } ).sort({"cuisine":-1});
14. Write a MongoDB query to find the Supplier Id, name, Location and Capacity for those Suppliers which contain 'Wil' as first three letters for its name . Solution: db.Suppliers.find( {name: /^Wil/}, { "Supplier_id" : 1, "name":1, "Location":1, “Capacity” :1 } );
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
15. Write a MongoDB query to find the Supplier Id, name, Location and Capacity for those Suppliers which contain 'ces' as last three letters for its name . Solution: db.Suppliers.find( {name: /ces$/}, { "Supplier_id" : 1, "name":1, "Location":1, “Capacity” :1 } );
16. Write a MongoDB query to find the Supplier Id, name, Location and Capacity for those Suppliers which contain 'Reg' as three letters somewhere in its name . Solution: db.Suppliers.find( {"name": /.*Reg.*/}, { "Supplier_id" : 1, "name":1, "Location":1, “Capacity” :1 } );
17. Write a MongoDB query to find the Suppliers which belong to the Location Calgary and prepared either Water tank or Coffee container dish. Solution: db.Suppliers.find( { "Location": "Calgary" , $or : [ { “Capacity” : "Water tank " }, { “Capacity” : "Coffee container" } ] } );
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
18. Write a MongoDB query to find the Supplier Id, name, Location and Capacity for those Suppliers which belong to the Location Staten Island or Queens or Calgary or Edmonton. Solution: db.Suppliers.find( {"Location" :{ $in :["Staten Island","Queens","Calgary","Edmonton"]}}, { "Supplier_id" : 1, "name":1,"Location":1, “Capacity” :1 } );
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
19. Write a MongoDB query to find the Supplier Id, name, Location and Capacity for those Suppliers which are not belonging to the Location Staten Island or Queens or Calgary or Edmonton. Solution: db.Suppliers.find( {"Location" :{ $nin :["Staten Island","Queens","Calgary","Edmonton"]}}, { "Supplier_id" : 1, "name":1,"Location":1, “Capacity” :1 } );
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
20. Write a MongoDB query to find the Supplier Id, name, Location and Capacity for those Suppliers which achieved a score which is not more than 10 . Solution: db.Suppliers.find( {"grades.score" : { $not: {$gt : 10} } }, { "Supplier_id" : 1, "name":1,"Location":1, “Capacity” :1 } );
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
21. Write a MongoDB query to find the Supplier Id, name, Location and Capacity for those Suppliers which prepared dish except 'Water tank ' and 'Coffee container' or Supplier's name begins with letter 'Wil' . Solution: db.Suppliers.find( $or: [ {name: /^Wil/}, {"$and": [ {“Capacity” : {$ne :"Water tank "}}, {“Capacity” : {$ne :"Coffee container"}} ]} ]}, {"Supplier_id " : 1,"name":1,"Location":1,”Capacity” :1} );
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
22. Write a MongoDB query which will select the Supplier Id, name and grades for those Suppliers which returns 0 as a remainder after dividing the score by 7 . Solution: db.Suppliers.find( {"grades.score" : { $mod : [7,0]} }, {"Supplier_id" : 1,"name":1,"grades":1} );
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
JOIN two collections There are two Collections “orders” and “inventory”. Below you can see the code snippet: db. orders .insertMany( [ { "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2 }, { "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1 }, { "_id" : 3 } ] ) db. inventory .insertMany( [ { "_id" : 1, "sku" : "almonds", "description": "product 1", "instock" : 120 }, { "_id" : 2, "sku" : "bread", "description": "product 2", "instock" : 80 }, { "_id" : 3, "sku" : "cashews", "description": "product 3", "instock" : 60 }, { "_id" : 4, "sku" : "pecans", "description": "product 4", "instock" : 70 }, { "_id" : 5, "sku": null, "description": "Incomplete" }, { "_id" : 6 } ] )
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
The following Aggregation operations perform Join operation on both these Collections and Join them using fields “ item ” from “ orders ” and “ sku ” from inventory ”. db. orders . aggregate ( [ { $lookup : { from: "inventory", localField: "item", foreignField: "sku", as: "inventory_docs } } ] )
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
The result is as follows: { "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2, "inventory_docs" : [ { "_id" : 1, "sku" : "almonds", "description" : "product 1", "instock" : 120 } ] } { "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1, "inventory_docs" : [ { "_id" : 4, "sku" : "pecans", "description" : "product 4", "instock" : 70 } ] } { "_id" : 3, "inventory_docs" : [ { "_id" : 5, "sku" : null, "description" : "Incomplete" }, { "_id" : 6 } ] }
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
THANK YOU
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help