Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
MongoDB Fundamentals
MongoDB Fundamentals

MongoDB Fundamentals: A hands-on guide to using MongoDB and Atlas in the real world

By Amit Phaltankar , Juned Ahsan , Michael Harrison , Liviu Nedov
€25.99 €17.99
Book Dec 2020 748 pages 1st Edition
eBook
€25.99 €17.99
Print
€32.99
Subscription
€14.99 Monthly
eBook
€25.99 €17.99
Print
€32.99
Subscription
€14.99 Monthly

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Dec 22, 2020
Length 748 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781839210648
Category :
Concepts :
Table of content icon View table of contents Preview book icon Preview Book

MongoDB Fundamentals

2. Documents and Data Types

Overview

This chapter introduces you to MongoDB documents, their structure, and data types. For those who are new to the JSON model, this chapter will also serve as a short introduction to JSON. You will identify the basic concepts and data types of JSON documents and compare the document-based storage of MongoDB with the tabular storage of relational databases. You will learn how to represent complex data structures in MongoDB using embedded objects and arrays. By the end of this chapter, you will understand the need for precautionary limits and restrictions on MongoDB documents.

Introduction

In the previous chapter, we learned how MongoDB, as a NoSQL database, differs from traditional relational databases. We covered the basic features of MongoDB, including its architecture, its different versions, and MongoDB Atlas.

MongoDB is designed for modern-world applications. We live in a world where requirements change rapidly. We want to build lightweight and flexible applications that can quickly adapt to these new requirements and ship them to production as quickly as possible. We want our databases to become agile so that they can adapt to the ever-changing needs of our applications, reduce downtime, scale out easily, and perform efficiently. MongoDB is a perfect fit for all such needs.

One of the major factors that make MongoDB an agile database is its document-based data model. Documents are widely accepted as a flexible way of transporting information. You might have come across many applications that exchange data in the form of JavaScript Object Notation...

Introduction to JSON

JSON is a full-text, lightweight format for data representation and transportation. JavaScript's simple representation of objects gave birth to JSON. Douglas Crockford, who was one of the developers of the JavaScript language, came up with the proposal for the JSON specification that defines the grammar and data types for the JSON syntax.

The JSON specification became a standard in 2013. If you have been developing applications for a while, you might have seen the transition of applications from XML to JSON. JSON offers a human-readable, plain-text way of representing data. In comparison to XML, where information is wrapped inside tags, and lots of tags make it look bulky, JSON offers a compact and natural format where you can easily focus on the information.

To read or write information in JSON or XML format, the programming languages use their respective parsers. As XML documents are bound by schema definitions and tag library definitions...

BSON

When you work with MongoDB using database clients such as mongo shell, MongoDB Compass, or the Collections Browser in Mongo Atlas, you always see the documents in human readable JSON format. However, internally, MongoDB documents are stored in a binary format called BSON. BSON documents are not human-readable, and you will never have to deal with them directly. Before we explore MongoDB documents in detail, let's have a quick overview of the BSON features that benefit the MongoDB document structure.

Like JSON, BSON was introduced in 2009 by MongoDB. Although it was invented by MongoDB, many other systems also use it as a format for data storage or transportation. BSON specifications are primarily based on JSON as they inherit all the good features of JSON, such as the syntax and flexibility. It also provides a few additional features, which are specifically designed for improving storage efficiency, ease of traversal, and a few data type enhancements to avoid the type...

MongoDB Documents

A MongoDB database is composed of collections and documents. A database can have one or more collections, and each collection can store one or more related BSON documents. In comparison to RDBMS, collections are analogous to tables and documents are analogous to rows within a table. However, documents are much more flexible compared with the rows in a table.

RDBMSes consist of a tabular data model that comprises rows and columns. However, your applications may need to support more complex data structures, such as a nested object or a collection of objects. Tabular databases restrict the storage of such complex data structures. In such cases, you will have to split your data into multiple tables and change the application's object structures accordingly. On the other hand, the document-based data model of MongoDB allows your application to store and retrieve more complex object structures due to the flexible JSON-like format of the documents.

The following...

MongoDB Data Types

You have learned how MongoDB stores JSON-like documents. You have also seen various documents and read the information stored within them and seen how flexible these documents are to store different types of data structures, irrespective of the complexity of your data.

In this section, you will learn about the various data types supported by MongoDB's BSON documents. Using the right data types in your documents is very important as correct data types help you use the database features more effectively, avoid data corruption, and improve data usability. MongoDB supports all the data types from JSON and BSON. Let's look at each in detail, with examples.

Strings

A string is a basic data type used to represent text-based fields in a document. It is a plain sequence of characters. In MongoDB, the string fields are UTF-8 encoded, and thus they support most international characters. The MongoDB drivers for various programming languages convert the string...

Limits and Restrictions on Documents

So far, we have discussed the importance and benefits of using documents. Documents play a major role in building efficient applications, and they improve overall data usability. We know how documents offer a flexible way to represent data in its most natural form. They are often self-contained and can hold a complete unit of information. The self-containment comes from nested objects and arrays.

To use any database effectively, it is important to have the correct data structure. The incorrect data structures you build today may result in lots of pain in the future. In the long term, as your application's usage grows, the amount of data also grows, and the problems that seemed very small initially become more evident. Then comes the obvious question: how do you know whether your data structure is correct?

Your application will tell you the answer. If, to access a certain piece of information, your application must execute multiple queries...

Field Name Rules

MongoDB has a few rules about document field names, which are listed as follows:

  1. The field name cannot contain a null character.
  2. Only the fields in an array or an embedded document can have a name starting with the dollar sign ($). For the top-level fields, the name cannot start with a dollar ($) sign.
  3. Documents with duplicate field names are not supported. According to the MongoDB documentation, when a document with duplicate field names is inserted, no error will be thrown, but the document won't be inserted. Even the drivers will drop the documents silently. On the mongo shell, however, if such a document is inserted, it gets inserted correctly. However, the resulting document will have only the second field. That means the second occurrence of the field overwrites the value of the first.

    Note

    MongoDB (as of version 4.2.8) does not recommend field names starting with a dollar ($) sign or a dot (.). The MongoDB query language may not work correctly...

Summary

In this chapter, we have covered a detailed structure of MongoDB documents and document-based models, which is important before we dive into more advanced concepts in the upcoming chapters. We began our discussion with the transportation and storage of information in the form of JSON-like documents that provide a flexible and language-independent format. We studied an overview of JSON documents, the document structure, and basic data types, followed by BSON document specifications and differentiating between BSON and JSON on various parameters.

We then covered MongoDB documents, considering their flexibility, self-containment, relatability, and agility, as well as various data types provided by BSON. Finally, we made a note of MongoDB's limitations and restrictions for documents and learned why the limitations are imposed and why they are important.

In the next chapter, we will use the mongo shell and Mongo Compass to connect to an actual MongoDB server and manage...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Learn the fundamentals of NoSQL databases with MongoDB
  • Create, manage, and optimize a MongoDB database in the cloud using Atlas
  • Use a real-world dataset to gain practical experience of handling big data

Description

MongoDB is one of the most popular database technologies for handling large collections of data. This book will help MongoDB beginners develop the knowledge and skills to create databases and process data efficiently. Unlike other MongoDB books, MongoDB Fundamentals dives into cloud computing from the very start – showing you how to get started with Atlas in the first chapter. You will discover how to modify existing data, add new data into a database, and handle complex queries by creating aggregation pipelines. As you progress, you'll learn about the MongoDB replication architecture and configure a simple cluster. You will also get to grips with user authentication, as well as techniques for backing up and restoring data. Finally, you'll perform data visualization using MongoDB Charts. You will work on realistic projects that are presented as bitesize exercises and activities, allowing you to challenge yourself in an enjoyable and attainable way. Many of these mini-projects are based around a movie database case study, while the last chapter acts as a final project where you will use MongoDB to solve a real-world problem based on a bike-sharing app. By the end of this book, you'll have the skills and confidence to process large volumes of data and tackle your own projects using MongoDB.

What you will learn

Set up and use MongoDB Atlas on the cloud Insert, update, delete, and retrieve data from MongoDB Build aggregation pipelines to perform complex queries Optimize queries using indexes Monitor databases and manage user authorization Improve scalability and performance with sharding clusters Replicate clusters, back up your database, and restore data Create data-driven charts and reports from real-time data

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Dec 22, 2020
Length 748 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781839210648
Category :
Concepts :

Table of Contents

15 Chapters
Preface Chevron down icon Chevron up icon
1. Introduction to MongoDB Chevron down icon Chevron up icon
2. Documents and Data Types Chevron down icon Chevron up icon
3. Servers and Clients Chevron down icon Chevron up icon
4. Querying Documents Chevron down icon Chevron up icon
5. Inserting, Updating, and Deleting Documents Chevron down icon Chevron up icon
6. Updating with Aggregation Pipelines and Arrays Chevron down icon Chevron up icon
7. Data Aggregation Chevron down icon Chevron up icon
8. Coding JavaScript in MongoDB Chevron down icon Chevron up icon
9. Performance Chevron down icon Chevron up icon
10. Replication Chevron down icon Chevron up icon
11. Backup and Restore in MongoDB Chevron down icon Chevron up icon
12. Data Visualization Chevron down icon Chevron up icon
13. MongoDB Case Study Chevron down icon Chevron up icon
Appendix Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.