Lab Sheet 07-Answers
Lab Sheet 07-Answers
Lab Sheet 7
NoSQL Document Databases
NoSQL document databases are based on a model that does not require SQL and tables, unlike
relational databases. Instead of using tables with the data types, columns, rows, schemas, and
tabular relations used in relational databases, NoSQL databases use documents with data types
descriptions and values. Groups of data or documents are known as collections and serve the same
purpose as tables in relational databases. Documents can have same or different structures
depending on what the user wants. Users can add new objects without modifying the entire
database. NoSQL databases use various encoding and formats to encode and encapsulate data. The
common encodings include XML, JSON, YAML and binary forms such as PDF, BSON, MS Excel
and MS Word. Document oriented databases allow users to store, retrieve, and manage data and
documents.
Document Databases with MongoDB
MongoDB is an open source next generation databases that lets users create applications never
before possible. It’s designed for CIOs, architects, DBAs and Ops, developers and Analysts.
MongoDB stores data using flexible document data model that is similar to JSON. Documents
contain one or more fields, including arrays, binary data and subdocuments. Fields can vary from
document to document. This flexibility allows development teams to evolve the data model rapidly
as their application requirements change. When users need to lock down their data model, optional
document validation enforces the rules they choose.
There are six simple concepts to understand about MongoDB.
1. It has the same concept of a database with which you are likely already familiar (or a
schema for you Oracle folks). Within a MongoDB instance you can have zero or more
databases, each acting as high-level containers for everything else.
2. A database can have zero or more collections. A collection shares enough in common with
a traditional table that you can safely think of the two as the same thing.
3. Collections are made up of zero or more documents. Again, a document can safely be
thought of as a row.
1 Lab07 - Answers
4. A document is made up of one or more fields, which you can probably guess are a lot like
columns.
6. Cursors are different than the other five concepts but they are important enough, and often
overlooked, that I think they are worthy of their own discussion. The important thing to
understand about cursors is that when you ask MongoDB for data, it returns a pointer to
the result set called a cursor, which we can do things to, such as counting or skipping ahead,
before actually pulling down data.
Robo 3T is a MongoDB management tool which is another open source software. It’s providing a
GUI for MongoDB.
Let’s start
2 Lab07 - Answers
Step 3: Create a new connection.
Test your connection before saving. After saving you will be connected with MongoDB through
your new connection.
3 Lab07 - Answers
Step 5: Create a new database called Books
4 Lab07 - Answers
Step 7: Insert, update, delete and view documents from DBBooks collection.
5 Lab07 - Answers
Step 9: Insert Document
Isbn Title Author Publication year price
111-111-1 'Professional Database 'Steve McConnell' 2005 320
Development'
111-111-2 'Database System Concepts' 'Ralph Johnson' 2008 250
db.DBooks.insert({
isbn: 111-111-1,
title: 'Professional Database Development',
author: 'Steve McConnell',
year: 2005,
price: 320
});
db.DBooks.insert({
isbn: 111-111-2,
title: 'Database System Concepts',
author: 'Ralph Johnson',
year: 2008,
price: 250
});
db.DBooks.insert({
isbn: 111-111-3,
title: 'Database Programming',
author: 'Benjamin C. Pierce',
year: 2005,
price: 150
});
6 Lab07 - Answers
db.DBooks.insert({
isbn: 111-111-4,
title: 'SQL Guide',
year: 2009,
price: 450
});
db.DBooks.insert({
isbn: 111-111-5,
title: 'Oracle Databases',
author: 'Ralph Johnson',
year: 2005,
price: 150
});
db.DBooks.insert({
isbn: 111-111-6,
title: 'My SQL Guide',
author: 'John Stephen',
year: 2007,
price: 250
});
Table view:
7 Lab07 - Answers
Step 10: Mastering selectors
In addition to the six concepts listed above, there’s one practical aspect of MongoDB named query
selectors. A MongoDB query selector is like the where clause of an SQL statement. As such, it
uses when finding, counting, updating and removing documents from collections. A selector is a
JSON object, the simplest of which is {} which matches all documents.
{field: value} is used to find any documents where field is equal to value.
Example: If we wanted to find all books published in year 2005, we could use
db.DBBooks.find({year : 2005})
Special operators;
Operator Meaning
$lt less than
$lte less than or equal
$gt greater than
$gte greater than or equal
$ne not equal
Example: get all books published in year 2005 that price more than 250$
db.DBBooks.find({year : 2005, price: {$gt: 250}})
8 Lab07 - Answers
Step 11:
1. Find all books that priced more than 150$.
db.getCollection('DBooks').find({price:{$gt: 150}})
Document View:
9 Lab07 - Answers
Table View:
Document View:
Table View:
10 Lab07 - Answers
3. Find books that published after year 2005.
db.getCollection('DBooks').find({year:{$gt: 2005}})
Document View:
Table View:
11 Lab07 - Answers
4. Find the books that authored by ‘Ralph Johnson’ and priced 150$ and above.
Table View:
12 Lab07 - Answers
Step 12: Updating Documents
Update takes two parameters. The selector (where) to use and what updates to apply to fields.
Assume the price of the book ‘Oracle Databases' has to be changed. The correct way to change
update the weight is using $set operation.
db.DBBooks.update({title: ‘Oracle
Databases'},{$set:{price: 400}})
Note:
Step 13:
5. Increase the price of book under the isbn ‘111-111-1’ by 100$.
Before;
13 Lab07 - Answers
6. Add ‘Robet Rounder’ as the author to the book under the isbn 111-111-4.
Document View:
Table View:
14 Lab07 - Answers
Step 14: Upserts
An upsert updates the document if found or inserts it if not. To enable upserting we pass a third
parameter to update as {upsert : true}. Example
db.DBBooks.update({},
{$set: {isbn: 111-111-10 }},
{multi:true});
15 Lab07 - Answers
Step 17:
7. Sort the books based on price decreasingly.
16 Lab07 - Answers
Additional – Sort books based on price increasingly
17 Lab07 - Answers
8. Count the number of books published in year 2005.
10. Count the number of books who have authored by 'Ralph Johnson'
References:
https://docs.mongodb.com/manual/tutorial/query-for-null-fields/
https://docs.mongodb.com/manual/reference/operator/update/inc/index.html
https://docs.mongodb.com/manual/reference/operator/update/sort/index.html
https://docs.mongodb.com/manual/reference/command/count/#dbcmd.count
https://www.tutorialspoint.com/mongodb/mongodb_sort_record
18 Lab07 - Answers