logo

Working with MongoDB: Database Structure and Querying

Create a MongoDB database and write a report for the Non-Relational Database Systems Task 3 assignment.

7 Pages1182 Words336 Views
   

Added on  2022-10-16

About This Document

This article discusses the database structure and querying in MongoDB. It covers how to handle relationships, work with queries, and add new fields. The advantages of using MongoDB are also highlighted, along with some recommendations for future improvements.

Working with MongoDB: Database Structure and Querying

Create a MongoDB database and write a report for the Non-Relational Database Systems Task 3 assignment.

   Added on 2022-10-16

ShareRelated Documents
Table of Contents
Introduction................................................................................................................ 2
Database structure..................................................................................................... 2
Working with queries.................................................................................................. 2
conclusion.................................................................................................................. 3
Working with MongoDB: Database Structure and Querying_1
Introduction
MongoDB is part of the NoSQL database system, and it is an open-source database
system, which provides easy scalability high performance and availability.
MongoDB provides a flexible format for storing data because it does not require its
document to have the same schema. Meaning fields in a single collection can differ
from a document to document.
Database structure
MongoDB have different ways of structuring documents in a collection. Unlike SQL
NoSQL allows related data to be embedded within a single document. We have used
this structure in organizing the data in a single collection. Embedding documents
allow the associated data to be accessed, retrieved and manipulated within a single
database.
We have created a single collection called the GameData. The collection contains
the fields of all the documents in an embedded structure. The structure makes it
easy to insert and retrieve data.
Handling Relationship in database
MongoDB use two approaches to create relationship between documents. The first
approach is embended approach. These methods use a single collection to store
data. For example, the developer information can be stored as a collection by itself
but instead of separating the files wi create an array for developer information and
embend it to the collection with game title. The approach is simple to implement
and use. The advantage of using the approach is that it requires a single query to
execute a query. Disadvantage it may become difficult to handle queries when the
data size is big.
The second approach is creating a referenced relationship. Here we create different
collection for each related data. For example, we can create a collection named
developer and reference the developer id to the game collection. The advantage is
that it is easy to manage the size of a document. On the other side we will need two
queries if we want torereive the developer details for a specific title.
Working with queries
The first step in creating the database was to add the provided information.
Entering data was a carefull process in order to avoid spelling mistakes and make
sure data had same datatype. To method were used in inserting data; insertOne
function and insert function. The insert function allows insertion of multiple data at
once.
Syntax
Db.gameData.insert ([{ key:”values”, key2: :value” }, {key:”values”,
key1:”values”}])
Working with MongoDB: Database Structure and Querying_2
The above code will add two data to the collection gameData.
Finding data is an essential feature for database. Just like SQL, MongoDB has a
method that allows data to be fetched from the database. To fetch all data we used
the find() method. This is how it is used.
Db.gameData.find().pretty()
The method will return all data in a JSON format. The pretty() function returns data
in an organized format that makes it easy to read and understand data. It is even
possible to find data using specific criteria. Below code was used to return all games
title whose price is zero dollars.
db.GameData.find({price:0},{title:1, _id:0}).pretty()
The first option defines the search criteria, while the second option indicates the
fields to be returned. The Id field is returned by default. To prevent it from being
Working with MongoDB: Database Structure and Querying_3

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
Json Schema Format Assignment-2022
|3
|803
|21

Big Data Database: MongoDB
|15
|689
|24

MongoDB:. Database created with the name – “facebookDB”
|7
|478
|21

Database Design for Games in MongoDB
|4
|1067
|418

Task-2 Report MongoDB for Movie Maniacs Database
|8
|1371
|154

Non-relational Database
|4
|820
|222