HCI NoSQL Databases 1. Introduction to NoSQL
Uploaded by adrianwang2003 · 28 May 2024
Preview
Text from the first pagesNoSQL Databases CPDD Computer Education Unit Version: Feb 2019 1 Name: __________________________ ( ) Class: _________ Date: _________ Lesson 1: Introduction to NoSQL Instructional Objectives: By the end of this task, you should be able to: Understand how NoSQL database management system address the shortcomings of relational database management system (SQL) Explain the applications of SQL and NoSQL Use MongoDB shell to create, edit and remove databases Why learn NoSQL? Relational databases (commonly referred as SQL databases) work well with structured data since each table's schema (the precise description of the data to be stored and the relationships between them ) is always clearly defined. However, with the increasing number of ways to gather and generate data , we often need to deal with unstructured data. For example , a convenience store that frequently refreshes the services it provides may sell both mobile phones as well as groceries. To run the store, information about both mobile phones (e.g., model names and prices) and groceries (e.g., prices and descriptions) need to be stored. In the future, the store may also start selling mobile phone subscription plans as well. Storing all this data in the same relational database may not be easy. In this case, non-relational databases, also referred to as NoSQL databases, can offer a better choice. There are four main types of NoSQL databases: key -value databases, document databases, wide-column databases and graph databases. For the syllabus, we focus on MongoDB, a type of document database. Document databases work like a hash table, but each key ca n point to an embedded key -value structure, also known as a document, instead of just a single value. (Recall that in a hash table, each key points to a single value or data item.) How NoSQL addresses shortcomings of relational databases Relational databases have a predefined schema that is difficult to change. Even if you wish to add a field to a small number of records, you still need to include the field for the entire table. Therefore, it can be difficult to support the processing of unstructured data using relational databases compared to NoSQL databases. Unlike NoSQL databases, relational databases do not usually support hierarchical data storage where less frequently-used data is moved to
NoSQL Databases CPDD Computer Education Unit Version: Feb 2019 2 cheaper, slower storage devices. This means that the cost of storing data in a relational database is more expensive than storing the same amount of data in a NoSQL database. Relational databases are mainly vertically scalable while NoSQL databases are mainly horizontally scalable. Vertically scalable means that improving the performance of a relational database server usually requires upgrading an existing server with faster processors and more memory. Such high - performance components can be expensive and upgrades are limited by the capacity of a single machine. On the other hand, horizontally scalable means that the performance of a NoSQL database can be improved by simply increasing the number of servers. This is relatively cheaper as mass-produced average-performance computers are easily available at low prices. Relational databases are stored in a server, which makes the database unavailable when the server fails. NoSQL databases are designed to take advantage of multiple servers so that if one server fails, the other servers can continue to support applications. Differences between relational databases and NoSQL databases Relational databases have a fixed, predefined schema that its tables follows but NoSQL databases usually have no predefined schema, which is dynamic and can change easily. Relational databases contain tables while NoSQL databases like MongoDB contain collections. The data types of each field in the table is fixed for relational databases but it is flexible for NoSQL databases like MongoDB. Relational databases represent dat a in tables and rows while NoSQL databases usually store data as collections of documents. For relational databases, joins are usually used to get data across tables, while for NoSQL databases like MongoDB there is usually no such joins. Thus it is easier to use relational databases for complex queries rather than NoSQL databases. Applications of SQL and NoSQL Databases The choice of whether to use a SQL or NoSQL database depends on the type of data being stored as well as the nature of tasks that the database is required to perform. SQL databases should be used if: The data being stored has a fixed schema. Complex and varied queries will be frequently performed. The atomicity, consistency, isolation and durability (ACID) properties are critical to the database. There will be a high number of simultaneous transactions.
NoSQL Databases CPDD Computer Education Unit Version: Feb 2019 3 NoSQL databases should be used if: The data being stored has a dynamic schema , (i.e., unstructured data with flexible data types). Data storage needs to be performed quickly. There will be an extremely large amount of data (i.e., Big Data). Terms used in MongoDB Here are the terms in MongoDB with the corresponding terms in SQL. MongoDB Term SQL Term Database Database Collection Table Document Row Field Column Introduction to MongoDB MongoDB is a NoSQL database. To start the MongoDB server, click on MongoDB Server icon in the Start menu. You will see a screen similar to the following.
NoSQL Databases CPDD Computer Education Unit Version: Feb 2019 4 Note! If you don’t see the icon, go to the MongoDB installation directory and create a shortcut for mongod on the desktop. Right-click on properties and add in --dbpath="D:\db", where D:\db is the directory storing the MongoDB database.
NoSQL Databases CPDD Computer Education Unit Version: Feb 2019 5 Use of MongoDB shell To interact with MongoDB databases, you need to start a connection to the server. To do so, use Windows Explorer and go to the MongoDB directory. Start mongo program. Starting the mongo shell program allows you to connect to the MongoDB server. You will then enter an interactive shell designed specifically for MongoDB. You can do use variables and mathematical operations similar to Python shell.
NoSQL Databases CPDD Computer Education Unit Version: Feb 2019 6 Go further! Look at the output on the MongoDB Server window as you type statements into the mongo shell, and try to figure out what the server is doing. You can create a database with the use statement. You can insert documents into a collection using insert statement. Try typing the following statements: use test_info db.person.insert({name:"John Lim",class:"18S01",hobbies:[ "running","kayaking","gaming"]}) The first statement tells MongoDB to use the database test_info. The second statement will insert a document into the person collection. Your screen will look like below. This states that one document has been inserted. Note! The database test_info is created only when a document is inserted. Note! The collection is au tomatically created when a document is added . You can manually create the person collection using the statement db.createCollection("person"). 1. What do you think is the output when you type the following? db.person.insert({name:"Ben",class:"18S01"}) WriteResult({"nInserted" : 1}) ……………………………………………………………………………………....... Check your answer by typing into MongoDB. 2. How many documents are there in the person collection now? 2 ……………………………………………………………………………………....... 3. Write down the statement to insert the student Mary from class 18S02 with hobby running into the person collection. db.person.insert({name:"Ma
Content continues in the PDF. Download PDF
Related notes
- NYJC 2026 Prelim P2Exam Papers · 2026
- NYJC 2026 Prelim P1Exam Papers · 2026
- DHS 2026 Y6 H2 Computing Prelim Paper 2_finalExam Papers · 2026
- ACJC 2026 JC2 Computing Prelim Paper 2 (Practical)Exam Papers · 2026
- 2026_NJC Prelim_Computing_P2.pdfExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P2_finalExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P1_markschemeExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P1_finalExam Papers · 2026
- 2026 ACJC Prelim Computing Paper 2Exam Papers · 2026
- 2024 ACJC Computing PromoExam Papers · 2024
- 2023 ACJC Promo QPExam Papers · 2023
- 2022 ACJC Computing Promo Paper 2Exam Papers · 2022
- See all H2 Computing notes

