Skip to content

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.

Image 01

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.

Image 02

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.

Image 03

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.

Image 04

In a below you can see one example connection string from MongoDB Compass to MongoDB Atlas Cloud.

1
mongodb+srv://pasi:******@democluster.brlob.mongodb.net/test

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.

Image 05

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.

Image 06

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).

Image 07

Add the values shown in the figure as a document.

Image 08

Finally, the first document in your collection should be visible.

Image 09

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.

Image 10

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
> show dbs
test  41kB
admin   340kB
local   3.44 GB

Change active database

1
2
> use test
switched to db test

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
> db
test

Get help

1
2
3
4
5
>  help
Shell Help
use    Set current database
show     'show databases'/'show dbs': Print
....

Create documents

Document person is created using JavaScript's Object notation.

1
person = { name : "Jussi Jurkka", email: "jussi@jurkka.fi" }

Let's add the document to the persons collection.

1
db.persons.insertOne(person)

You can add document without object creation.

1
db.persons.insertOne({ name : "Minna Husso", email: "minna@husso.fi" })

Note

Database/Collection will be created if those doesn't exists.

List all collections

1
2
> show collections
persons

Finding all documents

find method is used to search/verify that the documents are in the collection.

1
2
3
4
5
6
7
> db.persons.find()
{ _id: ObjectId("6357b92d0f7cf6032ab97f81"),
  name: 'Jussi Jurkka',
  email: 'jussi@jurkka.fi' }
{ _id: ObjectId("6357b9d60f7cf6032ab97f82"),
  name: 'Minna Husso',
  email: 'minna@husso.fi' }

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
for (var i = 1; i <= 10; i++) {
   db.testData.insert( { x : i } )
}

Find generated data - just testing it.

1
2
3
4
5
6
7
> db.testData.find()
{ "_id" : ObjectId("54d8b5eb06411fca9d3d7f7b"), "x" : 1 }
{ "_id" : ObjectId("54d8b5eb06411fca9d3d7f7c"), "x" : 2 }
{ "_id" : ObjectId("54d8b5eb06411fca9d3d7f7d"), "x" : 3 }
{ "_id" : ObjectId("54d8b5eb06411fca9d3d7f7e"), "x" : 4 }
{ "_id" : ObjectId("54d8b5eb06411fca9d3d7f7f"), "x" : 5 }
...

Use own made function

Create a following function.

1
2
3
4
5
6
7
function insertData(dbName, colName, num) {
  var col = db.getSiblingDB(dbName).getCollection(colName);
  for (i = 0; i < num; i++) {
    col.insert({x:i});
  }
  print(col.countDocuments());
}

Call your function.

1
2
> insertData("test", "testData", 400)
400

Data went to the database named test in the collection named testData, if you want to find it.

1
> use test
1
2
3
> show collections
< persons
  testData

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
let c = db.testData.find()

Use while to display all found documents:

1
while ( c.hasNext() ) printjson( c.next() )

Find documents, returns cursor object (all which has x value as 18):

1
2
> db.testData.find( { x : 18 } )
{ "_id" : ObjectId("54d8b92706411fca9d3d7fa6"), "x" : 18 }

Limit returned documents count:

1
2
3
4
> db.testData.find().limit(3)
{ "_id" : ObjectId("54d8b92706411fca9d3d7f94"), "x" : 0 }
{ "_id" : ObjectId("54d8b92706411fca9d3d7f95"), "x" : 1 }
{ "_id" : ObjectId("54d8b92706411fca9d3d7f96"), "x" : 2 }

Find one document, returns document (or null if not found) object:

1
2
> db.testData.findOne( { x : 18 } )
{ "_id" : ObjectId("54d8b92706411fca9d3d7fa6"), "x" : 18 }

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
{
  name: "sue",
  age: 26,
  status: "A",
  groups: [ "news", "sports" ]
}

Query

Query will target a specific collection of documents. It can contain criterias and modifiers.

1
2
> db.users.find( { age: {$gt: 18 } } ).sort( {age: 1 } )
COLLECTION=== QUERYCriteria========  MODIFIER=========

Insert

Inserting a new document:

1
2
3
4
5
6
7
8
> db.users.insertOne(
  {
    name: "sue",
    age: 26,
    status: "A",
    groups: [ "news", "sports" ]
  }
)

Reading

Reading a document:

1
2
3
4
5
6
7
> db.users.find()
{ _id: ObjectId("6357bc400f7cf6032ab982b7"),
  name: 'sue',
  age: 26,
  status: 'A',
  groups: [ 'news', 'sports' ] 
}

Updating

Updating document - all documents which age value is bigger than 18 set status to B

1
2
3
4
> db.users.updateMany(
  { age: { $gt: 18 } },
  { $set: { status: "B" } }
)

Removing

Remove document:

1
2
3
> db.users.remove(
  { status: "B" }
)

If you want to delete more documents, you can enter a suitable search condition, e.g. delete documents where x > 6:

1
2
3
> db.testData.remove(
  { x: { $gt: 6 } }
)

Upserting

Updating/Adding a document:

1
2
3
4
5
> db.users.update(
   { name: 'sue'},
   { $set: { status: "A" } },
   { upsert: true }
)

Note

  • Will add a document, if sue is not found
  • Updates a document, if sue is found

Saving

Saving a document (deprecated):

1
db.products.save( { _id: 100, item: "water", qty: 30 } )

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
> db.products.insertOne( { _id: 100, item: "water", qty: 30 } )

Aggregating

Find examples from here SQL to Aggregation Mapping Chart

Removing

Remove all documents from the collection:

1
db.testData.remove({})

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
{
   title: "MongoDB: The Definitive Guide",
   author: [ "Kristina Chodorow", "Mike Dirolf" ],
   published_date: ISODate("2010-09-24"),
   pages: 216,
   language: "English",
   publisher: {
              name: "O'Reilly Media",
              founded: 1980,
              location: "CA"
            }
}

{
   title: "50 Tips and Tricks for MongoDB Developer",
   author: "Kristina Chodorow",
   published_date: ISODate("2011-05-06"),
   pages: 68,
   language: "English",
   publisher: {
              name: "O'Reilly Media",
              founded: 1980,
              location: "CA"
            }
}

The publisher information is kept in a different collection than the book collection:

1
2
3
4
5
6
{
   name: "O'Reilly Media",
   founded: 1980,
   location: "CA",
   books: [12346789, 234567890, ...]
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
{
    _id: 123456789,
    title: "MongoDB: The Definitive Guide",
    author: [ "Kristina Chodorow", "Mike Dirolf" ],
    published_date: ISODate("2010-09-24"),
    pages: 216,
    language: "English"
}

{
   _id: 234567890,
   title: "50 Tips and Tricks for MongoDB Developer",
   author: "Kristina Chodorow",
   published_date: ISODate("2011-05-06"),
   pages: 68,
   language: "English"
}

Example

  • Many to many, A MessageBoard message can have many tags and the same tag can be used in many messages
1
2
db.messages.insertOne({ title: "My message!", tags: ["followme", "love"] })
db.messages.find({ tags: "followme" })
  • 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
let event = { _id: "lanparty",
              name: "LAN Party!",
              persons: ["arska", "pasi"] }
let person = { _id: "arska", name: "Ari Rantala", events: ["lanparty"]}
db.events.insertOne(event)
db.persons.insertOne(person)

db.events.find({_id: {$in: person.events}}) // person (arska) events
db.persons.find({_id: {$in: event.persons}})  // events (lanparty) persons