Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Install MongoDB on Ubuntu 18.04 and Quick Start Tutorial

In this post we will cover how to install Mongodb on Ubuntu 18.04.  MongoDB is a NoSQL database that stores information in JSON like objects called documents.

To get started make sure you have a fully updated clean install of Ubuntu Server 18.04.

The virtual machine I used for this post has 4 vCPUs, 4 GB of Memory, and 100 GB of drive space.

We will be installing MongoDB Community Edition.

Setup the MongoDB APT Repository

The mongodb package that comes with Ubuntu is not the one maintained by MongoDB so we will use their APT repository to install the version they do maintain.

# apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
# 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
# apt udpate

We are now ready to install the packages.

Install MongoDB on Ubuntu 18.04

Run the following command to install the latest version of MongoDB

# apt install mongodb-org -y

Now start and enable the service

# systemctl start mongod
# systemctl enable mongod

That’s all there is to installing MongoDB.

If you’re new to using MongoDB then keep reading while I give a short introduction.

Using MongoDB Shell

MongoDB comes with a CLI Shell that you can use to interact with it.

Run the following command to run the MongoDB Shell.

mongo --host 127.0.0.1:27017

As you can see MongoDB listens for connections on port 27017.

You will see the CLI Starting

root@mongodb:~# mongo --host 127.0.0.1:27017
MongoDB shell version v4.0.1
connecting to: mongodb://127.0.0.1:27017/
MongoDB server version: 4.0.1
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:
2018-08-13T02:59:20.983+0000 I STORAGE  [initandlisten]
2018-08-13T02:59:20.983+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-08-13T02:59:20.983+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-08-13T02:59:21.613+0000 I CONTROL  [initandlisten]
2018-08-13T02:59:21.613+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-08-13T02:59:21.613+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-08-13T02:59:21.613+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()
---

>

Creating new documents

Records in MongoDB are called documents which are JSON like objects that store your data.

When you insert a document into MongoDB for the first time you create the database.

Take for example this command:

> db.wowcharacters.insertOne( { name: "Belkas", server: "Malfurian", class: "Paladin" } )
{
        "acknowledged" : true,
        "insertedId" : ObjectId("5b70f4bb0b5de9d8d637d369")
}
>

When we run this command it creates the table (called a collection in MongoDB) wowcharacters and inserts the document into the collection.

Selecting documents

We can list all documents in a collection with the find() command:

> db.wowcharacters.find()
{ "_id" : ObjectId("5b70f4bb0b5de9d8d637d369"), "name" : "Belkas", "server" : "Malfurian", "class" : "Paladin" }
>

And we can see that we have indeed created our collection and our document exists.

Next, we will add a couple more documents.

> db.wowcharacters.insertMany( [{ name: "Tarish", server: "Kul Turis", class: "Warror" }, { name: "Krom", server: "Moonlight", class: "Rogue" }] )
{
        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("5b70f6370b5de9d8d637d36a"),
                ObjectId("5b70f6370b5de9d8d637d36b")
        ]
}

Now our find() function returns all three documents.

> db.wowcharacters.find()
{ "_id" : ObjectId("5b70f4bb0b5de9d8d637d369"), "name" : "Belkas", "server" : "Malfurian", "class" : "Paladin" }
{ "_id" : ObjectId("5b70f6370b5de9d8d637d36a"), "name" : "Tarish", "server" : "Kul Turis", "class" : "Warror" }
{ "_id" : ObjectId("5b70f6370b5de9d8d637d36b"), "name" : "Krom", "server" : "Moonlight", "class" : "Rogue" }

But what if we are searching for a particular character?  We can add query parameters to the find command.

> db.wowcharacters.find( {server: "Malfurian"} )
{ "_id" : ObjectId("5b70f4bb0b5de9d8d637d369"), "name" : "Belkas", "server" : "Malfurian", "class" : "Paladin" }

Now you can see we only get back the character that is on the Malfurian (Yes it is spelled incorrectly) server.

Updating documents

Next, lets update that document to have the correct spelling for the server name.

> db.wowcharacters.updateOne({name: "Belkas" }, { $set: { server: "Malfurion" } } )
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.wowcharacters.find( {name: "Belkas"} )
{ "_id" : ObjectId("5b70f4bb0b5de9d8d637d369"), "name" : "Belkas", "server" : "Malfurion", "class" : "Paladin" }

As you can see the server name now has the correct spelling.

Deleting Document

Next, let’s say that I quit playing the Tarish character and want to get rid of their record.

We can use the deleteOne() command to do this by selecting a record much like we have done in previous operations.

> db.wowcharacters.deleteOne( {name: "Tarish"} )
{ "acknowledged" : true, "deletedCount" : 1 }
> db.wowcharacters.find()
{ "_id" : ObjectId("5b70f4bb0b5de9d8d637d369"), "name" : "Belkas", "server" : "Malfurion", "class" : "Paladin" }
{ "_id" : ObjectId("5b70f6370b5de9d8d637d36b"), "name" : "Krom", "server" : "Moonlight", "class" : "Rogue" }

There we can see that our Tarish character has been deleted.

Conclusion

In this article I have shown how to install MongoDB on Ubuntu 18.04.

In addition, I showed you how to perform basic database operations in MongoDB.

You can find other great Ubuntu articles on AdminTome Blog here.

The post Install MongoDB on Ubuntu 18.04 and Quick Start Tutorial appeared first on AdminTome Blog.



This post first appeared on AdminTome, please read the originial post: here

Share the post

Install MongoDB on Ubuntu 18.04 and Quick Start Tutorial

×

Subscribe to Admintome

Get updates delivered right to your inbox!

Thank you for your subscription

×