0% found this document useful (0 votes)
128 views

Lab Sheet 07-Answers

NoSQL document databases store data in flexible documents rather than rigid tables. Documents can have varying structures and new objects can be added without modifying the entire database. MongoDB is an open-source document database that stores data as JSON-like documents with fields that can vary across documents. Robo 3T is a GUI tool for managing MongoDB databases and collections. This lab demonstrates how to use Robo 3T to connect to a MongoDB instance, insert, query, update, and delete documents in a books database and collection.

Uploaded by

Nuwan Chamikara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views

Lab Sheet 07-Answers

NoSQL document databases store data in flexible documents rather than rigid tables. Documents can have varying structures and new objects can be added without modifying the entire database. MongoDB is an open-source document database that stores data as JSON-like documents with fields that can vary across documents. Robo 3T is a GUI tool for managing MongoDB databases and collections. This lab demonstrates how to use Robo 3T to connect to a MongoDB instance, insert, query, update, and delete documents in a books database and collection.

Uploaded by

Nuwan Chamikara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Data Science and Analytics (IT3080)

B.Sc (Hons) in IT (IT Specialization)


Year 3

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.

5. Indexes in MongoDB function mostly like their RDBMS counterparts.

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

Step1: Start MongoDB Server.


Open command prompt from bin folder inside MongoDB installation and execute mongodb
application.

Step 2: Start Robo 3T.

2 Lab07 - Answers
Step 3: Create a new connection.

Step 4: Provide the connection settings.

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

Step 6: Create a collection called DBBooks inside Books database.

4 Lab07 - Answers
Step 7: Insert, update, delete and view documents from DBBooks collection.

Step 8: Open a shell to type commands.

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

111-111-3 'Database Programming' 'Benjamin C. Pierce' 2005 150

111-111-4 'SQL Guide' 2009 450

111-111-5 'Oracle Databases' 'Ralph Johnson' 2005 150

111-111-6 'My SQL Guide' 'John Stephen' 2007 250

Insert DBBooks details provided with DBBooks.txt file to DBBooks collection.

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:

2. Find all books that have no author field.


db.getCollection('DBooks').find({author: null})

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.

db.getCollection('DBooks').find({author: 'Ralph Johnson', price:{$gte: 150}})


Document View:

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:

$inc operator is used to increment a field by a certain positive or negative amount.


$push operator is used to add a value to the existing field.

Step 13:
5. Increase the price of book under the isbn ‘111-111-1’ by 100$.

db.DBooks.update({isbn: 111-111-1 }, {$inc:{price:100}})

Before;

After increase price by 100$;

13 Lab07 - Answers
6. Add ‘Robet Rounder’ as the author to the book under the isbn 111-111-4.

db.DBooks.update({isbn: 111-111-4 }, {$set:{author:'Robet Rounder'}})

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({title: ‘Oracle Databases' },


{$set:{price: 400}}, {upsert : true})

Step 15: Multiple Update


Multiple records are updated using;

db.DBBooks.update({},
{$set: {isbn: 111-111-10 }},
{multi:true});

Step 16: Deleting Documents


In order to remove any document inside the collection you can use:
db.DBBooks.remove({})

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.

9. Update the publication year of all books to 2010.

db.DBooks.update({}, {$set: {year: 2010}}, {multi:true})

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

You might also like