Install MongoDB Community Edition on Ubuntu 18.04 LTS (bionic)
The following tutorial uses a package manager to install MongoDB 4.0 Community Edition on Ubuntu 18.04 LTS (bionic) systems.
TL;DR
{
wget -qO - https://www.mongodb.org/static/pgp/server-4.0.asc | sudo apt-key add -
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
sudo apt update
sudo apt install mongodb-org -y
sudo service mongod start
}
Verify that the mongod process has started successfully by checking the contents of the log file at /var/log/mongodb/mongod.log for a line reading [initandlisten] waiting for connections on port 27017
sudo tail /var/log/mongodb/mongod.log | grep waiting
2019-08-07T14:23:19.938+0000 I NETWORK [initandlisten] waiting for connections on port 27017
Install MongoDB Community Edition using .deb Packages
Check your Ubuntu version
lsb_release -dc
Description: Ubuntu 18.04.2 LTS
Codename: bionic
Import the public key used by the package management system
From a terminal, issue the following command to import the MongoDB public GPG Key from https://www.mongodb.org/static/pgp/server-4.0.asc:
wget -qO - https://www.mongodb.org/static/pgp/server-4.0.asc | sudo apt-key add -
The operation should respond with an OK.
OK
Create a list file for MongoDB
Create the list file /etc/apt/sources.list.d/mongodb-org-4.0.list for Ubuntu 18.04 (Bionic):
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse
Reload local package database.
Issue the following command to reload the local package database:
sudo apt update
Hit:1 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic InRelease
Get:2 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:3 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:4 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:5 http://archive.canonical.com/ubuntu bionic InRelease [10.2 kB]
Ign:6 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 InRelease
Get:7 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic/universe amd64 Packages [8570 kB]
Get:8 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 Release [2991 B]
Get:9 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 Release.gpg [801 B]
Get:10 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic/universe Translation-en [4941 kB]
Get:11 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic/multiverse amd64 Packages [151 kB]
Get:12 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic/multiverse Translation-en [108 kB]
Get:13 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [697 kB]
Get:14 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-updates/main Translation-en [255 kB]
Get:15 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [7024 B]
Get:16 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [983 kB]
Get:17 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-updates/universe Translation-en [299 kB]
Get:18 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [11.9 kB]
Get:19 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-updates/multiverse Translation-en [5764 B]
Get:20 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-backports/main amd64 Packages [2512 B]
Get:21 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-backports/main Translation-en [1644 B]
Get:22 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-backports/universe amd64 Packages [3732 B]
Get:23 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic-backports/universe Translation-en [1696 B]
Get:24 http://archive.canonical.com/ubuntu bionic/partner amd64 Packages [2320 B]
Get:25 http://archive.canonical.com/ubuntu bionic/partner Translation-en [1276 B]
Get:26 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [464 kB]
Get:27 http://security.ubuntu.com/ubuntu bionic-security/main Translation-en [160 kB]
Get:28 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [577 kB]
Get:29 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0/multiverse amd64 Packages [8313 B]
Get:30 http://security.ubuntu.com/ubuntu bionic-security/universe Translation-en [189 kB]
Get:31 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [4008 B]
Get:32 http://security.ubuntu.com/ubuntu bionic-security/multiverse Translation-en [2060 B]
Fetched 17.7 MB in 3s (5172 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
92 packages can be upgraded. Run 'apt list --upgradable' to see them.
Install the MongoDB packages
You can install either the latest stable version of MongoDB or a specific version of MongoDB.
To install the latest stable version, issue the following
sudo apt install mongodb-org -y
...
...
Adding system user `mongodb' (UID 112) ...
Adding new user `mongodb' (UID 112) with group `nogroup' ...
Not creating home directory `/home/mongodb'.
Adding group `mongodb' (GID 116) ...
Done.
Adding user `mongodb' to group `mongodb' ...
Adding user mongodb to group mongodb
Done.
Setting up mongodb-org (4.0.11) ...
Run MongoDB Community Edition
Start MongoDB
mongod is the primary daemon process for the MongoDB system. It handles data requests, manages data access, and performs background management operations.
Issue the following command to start mongod:
sudo service mongod start
Verify that MongoDB has started successfully
Verify that the mongod process has started successfully by checking the contents of the log file at /var/log/mongodb/mongod.log for a line reading [initandlisten] waiting for connections on port 27017
27017 is the default port the standalone mongod listens on.
You may see non-critical warnings in the mongod output. As long as you see the log line shown above, you can safely ignore these warnings during your initial evaluation of MongoDB.
sudo tail /var/log/mongodb/mongod.log
2019-08-07T13:16:15.962+0000 I CONTROL [initandlisten]
2019-08-07T13:16:15.963+0000 I STORAGE [initandlisten] createCollection: admin.system.version with provided UUID: 38e2a204-8676-452a-b31a-1a9edd5985fd
2019-08-07T13:16:15.974+0000 I COMMAND [initandlisten] setting featureCompatibilityVersion to 4.0
2019-08-07T13:16:15.977+0000 I STORAGE [initandlisten] createCollection: local.startup_log with generated UUID: a6648377-0a8c-4788-907f-cf3221d300fc
2019-08-07T13:16:15.989+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/var/lib/mongodb/diagnostic.data'
2019-08-07T13:16:15.990+0000 I NETWORK [initandlisten] waiting for connections on port 27017
2019-08-07T13:16:15.990+0000 I STORAGE [LogicalSessionCacheRefresh] createCollection: config.system.sessions with generated UUID: 52750256-3836-46e1-b83f-97b2e646754e
2019-08-07T13:16:16.009+0000 I INDEX [LogicalSessionCacheRefresh] build index on: config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 }
2019-08-07T13:16:16.009+0000 I INDEX [LogicalSessionCacheRefresh] building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2019-08-07T13:16:16.010+0000 I INDEX [LogicalSessionCacheRefresh] build index done. scanned 0 total records. 0 secs
Begin using MongoDB
mongo is an interactive JavaScript shell interface to MongoDB, which provides a powerful interface for system administrators as well as a way for developers to test queries and operations directly with the database. mongo also provides a fully functional JavaScript environment for use with a MongoDB. The mongo shell is part of the MongoDB distributions.
Start a mongo shell on the same host machine as the mongod. You can run the mongo shell without any command-line options to connect to a mongod that is running on your localhost with default port 27017:
mongo
MongoDB shell version v4.0.11
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("27f80de1-98fd-4628-9487-5636c3ac9996") }
MongoDB server version: 4.0.11
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2019-08-07T13:16:15.304+0000 I STORAGE [initandlisten]
2019-08-07T13:16:15.304+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-08-07T13:16:15.304+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-08-07T13:16:15.962+0000 I CONTROL [initandlisten]
2019-08-07T13:16:15.962+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-08-07T13:16:15.962+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-08-07T13:16:15.962+0000 I CONTROL [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
>
Getting Started with MongoDB CRUD Operations
Create
db.collection.insertOne() inserts a single document into a collection.
db.inventory.insertOne({
item: "canvas",
qty: 100,
tags: ["cotton"],
size: { h: 28, w: 35.5, uom: "cm" }
});
{
"acknowledged": true,
"insertedId": ObjectId("5d4ad0b32df60d2af78da2ac")
}
Read
To retrieve the document that you just inserted, query the collection:
db.inventory.find({ item: "canvas" });
{
"_id": ObjectId("5d4ad0b32df60d2af78da2ac"),
"item": "canvas",
"qty": 100,
"tags": ["cotton"],
"size": { "h": 28, "w": 35.5, "uom": "cm" }
}
Update
The following example uses the db.collection.updateOne() method on the inventory collection to update the first document where item equals "canvas":
db.inventory.updateOne(
{ item: "canvas" },
{
$set: { status: "P" },
$currentDate: { lastModified: true }
}
);
{ "acknowledged": true, "matchedCount": 1, "modifiedCount": 1 }
Retreive the document and verify the update
db.inventory.find({ item: "canvas" });
{
"_id": ObjectId("5d4ad0b32df60d2af78da2ac"),
"item": "canvas",
"qty": 100,
"tags": ["cotton"],
"size": { "h": 28, "w": 35.5, "uom": "cm" },
"lastModified": ISODate("2019-08-07T13:24:42.233Z"),
"status": "P"
}
Delete
The following example deletes all documents from the inventory collection:
db.inventory.deleteMany({});
{ "acknowledged": true, "deletedCount": 1 }
To select all documents in the collection, pass an empty document as the query filter parameter to the find method.
db.inventory.find({});
You will get no response