M08 - MongoDB
MongoDB
MongoDB is an open-source document database built on a horizontal scale-out architecture. Founded in 2007, MongoDB has a worldwide following in the developer community.
MongoDB is a document-oriented NoSQL database used for high volume data storage. MongoDB uses collections and documents, instead of tables and rows which are used in the traditional relation databases, like MySQL.
In MongoDB each database contains collections, which in turn contains documents. Each of these documents can contains different number of fields which contains key-value pairs, which are the basic unit of data in MongoDB.
MongoDB is a popular choice as a database with Node.js and Express, e.g. in MEAN and MERN stacks. Therefore, it is a natural database choice for this course. This material focuses on accessing MongoDB through the Mongo Shell command-line interface, as the goal is to write JavaScript programming code using Node.js and Express to manipulate the MongoDB database.
Free courses
Mongo University offers free courses designed to equip you with the skills you need to succeed. Courses contains downloadable video lectures, Hands-on labs and quizzes and active discussion forums. With the completion of each course, you will receive a Proof of Completion to help advance your career.
Links
Mongo Basics
In this course you will learn how to set up your database and start exploring different ways to search, create, and analyze your data with MongoDB. We will cover database performance basics, and discover how to get started with creating applications and visualizing your data.
MongoDB for Javascript Developers
This course will teach you how to use MongoDB as the database for a Node.js application. You will play the role of a back-end developer for a Node.js application, where your job is to implement the application's communication with MongoDB. Using the Node.js driver you will read and write data to the database, use the aggregation framework, manage the configuration of the database client, and create a robust application by handling exceptions and timeouts.
MongoDB Compass
Compass is an interactive tool for querying, optimizing, and analyzing your MongoDB data. Get key insights, drag and drop to build pipelines, and more. Easily work with your data in Compass, the GUI built by — and for — MongoDB. Compass provides everything from schema analysis to index optimization to aggregation pipelines in a single, centralized interface.
Download it from here: MongoDB Compass.
Local computer
You can install MongoDB locally to your computer or use MongoDB in the Cloud using Atlas. MongoDB supports a variety of 64-bit platforms. Refer to the Supported Platforms table to verify that MongoDB is supported on the platform to which you wish to install it.
MongoDB offers both an Enterprise and Community version of its powerful distributed document database. You should install Community version to your local computer, which is free to use.
MongoDB cloud
The version of the MongoDB cloud service can be found at: https://www.mongodb.com/, where you can register for free. This is one easy way to include the database provided by MongoDB in your own applications. You don't have to set up and maintain it yourself. Another significant advantage is that MongoDB is accessible from all your published applications. MongoDB on your own local machine on localhost is available only during application coding time.
Get Started with Atlas instructions can be found from here: Get Started with Atlas. Basicly you will need to follow below steps to get your database to Mongo Cloud / Atlas UI:
- Create an Atlas account
- Create and deploy a free cluster (select free shared, for example AWS and Frankfurt)
- Add your connection IP address to your IP access list
- Create a database user for your cluster
- Connect to your cluster, for example with Mongo Compass, or Node app
Connections to MongoDB
Connection can be made a few different ways:
- using a MongoDB Shell (that black cmd window in Windows)
- using a MongoDB Compass application
- using some own made application, for example from Node based application
You can find out the addresses of these connections at the end of defining the connections in MongoDB Atlas Cloud. Select your Cluster and click Connect button and select some of the connections.
In a below you can see one example connection string from MongoDB Compass to MongoDB Atlas Cloud.
1 |
|
This address can be used in connection with the MongoDB application. Start (if necessary, install the MongoDB Compass application) and use your own connection address. Finally, press the Connect button.
You should see your own database in Mongo Atlas. Please note that you currently do not have a single collection (Collections) in the service. You will learn how to create/edit/delete/... these collections later.
Click on the name of your cluster on the Mongo Atlas Web page and you will see many different views as tabbed options. One of the most important is the "Collections" tab. This tab shows all the collections that were in the database. Now it's still empty. You can bring information to the database or create your own. Let's test a little and create some information.
Select "Add My Own Data" from the view (note that this selection can be different when you are reading this documentation). Add a test
database and a persons
collection.
Press Create button and a new database will be created in the service. The currently created persons collection is still empty. If you are using the MongoDB Compass application, refresh the view there as well and the empty persons collection will be visible.
Modify content
You can modify your database content a few different ways:
- using MongoDB Cloud UI
- using MongoDB Compass application
- using MongoDB Shell or
- using your own code from example from Node based app
Next, let's add some data using MongoDB's Atlas view. Select the "Collections" tab from your own cluster and press the "INSERT DOCUMENT" button in the view of the test.persons
collection (on the right side, rather in the middle).
Add the values shown in the figure as a document.
Finally, the first document in your collection should be visible.
Tip
Similarly, you can try adding information using the MongoDB Compass application.
Using MongoDB Compas Mongo Shell
The MongoDB Compass application also includes its own Mongo Shell command line window, which is used as shown in the attached image.
This material demonstrates the functionality of MongoDB using the Mongo Shell command line window of the MongoDB Compass application. The presented commands are therefore meant to be written on the command line marked in red above. Mongo Shell includes a full functional environment to JavaScript and connections to selected MongoDB.
Examples
The examples in this section are intended to be tested in order of presentation. The commands intended to be tried are either 1) on the lines starting with the command prompt as follows or 2) intended to be connected to Mongo Shell as such, as pure JavaScript program codes.
List all databases
1 2 3 4 |
|
Change active database
1 2 |
|
A database switched to active does not have to exist. Such a database is then created if data is added to it. The creation of a new database is therefore done by changing it to active (inventing a name) and adding data to it.
Checking database
1 2 |
|
Get help
1 2 3 4 5 |
|
Create documents
Document person
is created using JavaScript's Object notation.
1 |
|
Let's add the document to the persons collection.
1 |
|
You can add document without object creation.
1 |
|
Note
Database/Collection will be created if those doesn't exists.
List all collections
1 2 |
|
Finding all documents
find
method is used to search/verify that the documents are in the collection.
1 2 3 4 5 6 7 |
|
Note
All MongoDB documents has a own unique _id
, which will be generated automatically!
Generate test data
Use for loop to generate test data. A new testData
collection will be created.
1 2 3 |
|
Find generated data - just testing it.
1 2 3 4 5 6 7 |
|
Use own made function
Create a following function.
1 2 3 4 5 6 7 |
|
Call your function.
1 2 |
|
Data went to the database named test
in the collection named testData
, if you want to find it.
1 |
|
1 2 3 |
|
Cursor
- By default, Mongo's query returns a
cursor
object that contains the results of the query - Mongo Shell shows (by default) 20 instances at once
- Results Browse with the
it
command - If desired, the data can be displayed in larger or smaller blocks
Find all documents:
1 |
|
Use while
to display all found documents:
1 |
|
Find documents, returns cursor object (all which has x value as 18):
1 2 |
|
Limit returned documents count:
1 2 3 4 |
|
Find one document, returns document (or null if not found) object:
1 2 |
|
CRUD operations
You can create a CRUD = Create, Read, Update, Delete, etc... operations to your MongoDB. Find documentation from here: MongoDB CRUD Operations.
A document is a single record in a MongoDB collection consisting of key-value pairs in JSON format:
1 2 3 4 5 6 |
|
Query
Query will target a specific collection of documents. It can contain criterias and modifiers.
1 2 |
|
Insert
Inserting a new document:
1 2 3 4 5 6 7 8 |
|
Reading
Reading a document:
1 2 3 4 5 6 7 |
|
Updating
Updating document - all documents which age
value is bigger than 18 set status to B
1 2 3 4 |
|
Removing
Remove document:
1 2 3 |
|
If you want to delete more documents, you can enter a suitable search condition, e.g. delete documents where x > 6:
1 2 3 |
|
Upserting
Updating/Adding a document:
1 2 3 4 5 |
|
Note
- Will add a document, if
sue
is not found - Updates a document, if
sue
is found
Saving
Saving a document (deprecated):
1 |
|
Note
- Will change a document, if
_id:100
is found - Will add a new document if
_id:100
is not found or_id
is not used at all in document
Use insertOne
to add a new document
1 |
|
Aggregating
Find examples from here SQL to Aggregation Mapping Chart
Removing
Remove all documents from the collection:
1 |
|
MongoDB Data Model Design
Effective data models support your application needs. The key consideration for the structure of your documents is the decision to embed or to use references. You can find more information about this from here Data Model Design.
Tip
We are not going a deep level with database designing. Only o few example will be shown.
Example
- one-to-many, one publisher can publish many books
- publisher multiple times <- not good
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
The publisher information is kept in a different collection than the book collection:
1 2 3 4 5 6 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Example
- Many to many, A MessageBoard message can have many tags and the same tag can be used in many messages
1 2 |
|
- Many to many, Many people can participate in a party, and a person can participate in many parties
1 2 3 4 5 6 7 8 9 |
|