Prologue
Hello everyone, back to the “What I know about MongoDB” series of articles, a series about an extremely popular NoSQL DB, MongoDB.
In the content of this article, I will share how to install mongodb as standalone and some basic operations with mongodb such as creating database, creating collection, adding editing and deleting basic data…
Instructions for installing mongodb
MongoDB can be installed on many platforms, from installing packages on operating systems such as Linux, MacOS, Window.. to installing using docker or installing on Kubernetes via helmcharts.
In this content, I will show you how to install mongodb on Linux and specifically Ubuntu version 20.04. The installation on other OSes is basically not too different, you can refer to it here
Install mongodb on ubuntu 20.04
I will install MongoDB 6.0 Community Edition on Ubuntu 20.04 LTS Focal. The installation procedure is quite simple, follow the steps below!
Step 1: Add the key for the repo to install mongodb
1 2 | wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add - |
Return result ” OK ” is success. If there is an error, please perform the following additional steps and then repeat the above step:
1 2 3 | sudo apt-get install -y gnupg wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add - |
Step 2: Add the key for the repo to install mongodb
1 2 | echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list |
Step 3: Update the local repo again
1 2 | sudo apt update -y |
Step 4: Install package of mongodb
1 2 | sudo apt-get install -y mongodb-org |
Once saved when installing with a package on OS, we need to mark this installation package as not automatically updating when updating the repo by the following way:
1 2 3 4 5 6 7 | echo "mongodb-org hold" | sudo dpkg --set-selections echo "mongodb-org-database hold" | sudo dpkg --set-selections echo "mongodb-org-server hold" | sudo dpkg --set-selections echo "mongodb-mongosh hold" | sudo dpkg --set-selections echo "mongodb-org-mongos hold" | sudo dpkg --set-selections echo "mongodb-org-tools hold" | sudo dpkg --set-selections |
Launch mongodb
Note about ulimit . configuration
You need to set the ulimit value for max-open-files to at least 64000 otherwise the mongod service will give an error. The ulimit value is set for each user of the OS, so it needs to be set correctly for the user used to run mongodb.
When installing with package manager like using apt on ubuntu above, the ulimit value has been set automatically, you can verify it again as follows:
Step 1: Check the PID of mongod
1 2 3 | root@mongodb-standalone:/etc/systemd# ps -ef |grep mongod |grep -v grep mongodb 475 1 0 14:59 ? 00:00:04 /usr/bin/mongod --config /etc/mongod.conf |
Step 2: Check the ulimit values of the above process (in this case PID=475)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | root@mongodb-standalone:/etc/systemd# cat /proc/475/limits Limit Soft Limit Hard Limit Units Max cpu time unlimited unlimited seconds Max file size unlimited unlimited bytes Max data size unlimited unlimited bytes Max stack size 8388608 unlimited bytes Max core file size 0 unlimited bytes Max resident set unlimited unlimited bytes Max processes 64000 64000 processes Max open files 64000 64000 files Max locked memory unlimited unlimited bytes Max address space unlimited unlimited bytes Max file locks unlimited unlimited locks Max pending signals 7837 7837 signals Max msgqueue size 819200 819200 bytes Max nice priority 0 0 Max realtime priority 0 0 Max realtime timeout unlimited unlimited us |
You will see that the value of the parameter ” Max open files ” is being set to 64000.
Configuration of service mongod
When installing the mongodb-org
package, the default mongod
service is created and defined by the service configuration file at /lib/systemd/system/mongod.service
.
In this config file contains the parameters to run this service, in which the important parameter is ExecStart
:
1 2 | ExecStart=/usr/bin/mongod --config /etc/mongod.conf |
And you can see that the main config file to run this service is /etc/mongod.conf
. You can completely customize the path to the service’s config file, as well as customize the parameters in this config file:
1 2 3 4 5 6 7 8 9 10 | storage: dbPath: /var/lib/mongodb systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log net: port: 27017 bindIp: 127.0.0.1 |
Some important configuration parameters to note:
storage
: Path to the directory containing the data of mongodbsystemLog
: Path to the directory containing the log of this mongod servicenet
: Declare the Port and IP to bindport. Default will only bind Port (default 27017) to localhost (127.0.0.1) so standing from another host will not be able to connect to the db if you do not bind to other interfaces of the host.
Run mongodb
After checking and customizing the configuration parameters (if desired), we can start mongodb. If you have edited the service configuration file, you need to reload it with the command:
1 2 | sudo systemctl daemon-reload |
Then start mongodb:
1 2 3 | sudo systemctl enable mongod sudo systemctl start mongod |
The result when mongod starts successfully is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 | ubuntu@mongodb-standalone:~$ sudo service mongod status ● mongod.service - MongoDB Database Server Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2023-03-18 14:59:01 UTC; 30min ago Docs: https://docs.mongodb.org/manual Main PID: 475 (mongod) Memory: 229.2M CGroup: /system.slice/mongod.service └─475 /usr/bin/mongod --config /etc/mongod.conf Mar 18 14:59:01 mongodb-standalone systemd[1]: Started MongoDB Database Server. |
Start using mongodb
Now that mongodb has been started, you can start connecting to the db by running the mongosh
command on the server you are installing mongodb on.
1 2 3 4 5 6 7 8 | ubuntu@mongodb-standalone:~$ mongosh Current Mongosh Log ID: 6415d80bfe9ee8d719c45af1 Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.0 Using MongoDB: 6.0.5 Using Mongosh: 1.8.0 For mongosh info see: https://docs.mongodb.com/mongodb-shell/ test> |
So we have connected to mongodb and by default will connect to the database named test
.
Basic operations with mongodb
First of all, it should be recalled that some concepts on mongodb will be different from those in other Rational Databases such as:
So here we need to remember the organization in mongodb will be: database => collection => document => field.
Create new database in mongodb with mongosh
When I first connect to mongodb, I am connected to a white db called test
. To see the list of databases available in the system, use the command:
1 2 3 4 5 | test> show dbs admin 40.00 KiB config 48.00 KiB local 72.00 KiB |
Note that the test
db is a blank db so it will not be listed in the show dbs
statement.
To create a new DB, execute the command use [database-name]
:
1 2 3 4 | test> use demo switched to db demo demo> |
Thus, a blank DB named demo
is created and we will also be automatically transferred to that demo
DB. In the command prompt will tell us which database we are working on
Create new collection in database
There are 2 ways to create collections in mongodb:
- Create with createCollection . command
- Created by inserting data into the collection (collection will be created if it doesn’t already exist)
Create a collection using the createCollection . command
We can create a new Collection named staff
as follows:
1 2 3 | demo> db.createCollection("staff") { ok: 1 } |
Create a collection with the insertOne command
1 2 3 4 5 6 | demo> db.demo_collection.insertOne({name: "Trinh Quoc Viet", age: "18", gender: "male", cert: ["AWS", "Azure"] }) { acknowledged: true, insertedId: ObjectId("6415dc28914137d28a613d06") } |
Insert into collection
To add a document to the collection, use the insertOne command
1 2 3 4 5 6 | demo> db.staff.insertOne({name: "Nguyen Hoang Anh", age: "21", gender: "male", cert: ["GCP", "Azure"] }) { acknowledged: true, insertedId: ObjectId("6415e280914137d28a613d0b") } |
To add multiple documents to the collection, use the insertMany command:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | db.staff.insertMany([ { name: "Trinh Quoc Viet", age: "18", gender: "male", cert: ["AWS", "Azure"] }, { name: "Nguyen Van Anh", age: "20", gender: "female", cert: ["AWS", "CKA", "CKAD"] } ]) |
Result:
1 2 3 4 5 6 7 8 | { acknowledged: true, insertedIds: { '0': ObjectId("6415df0b914137d28a613d08"), '1': ObjectId("6415df0b914137d28a613d09") } } |
Query data in the collection (find function)
There are 2 ways to use find in mongodb: find() and findOne()
Using find()
To select data from mongodb collection, we can use find() function. This function can take as input a query object to filter data. If this filter condition is empty, all documents of this collection will be returned.
Example select all from the staff collection:
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 26 27 | demo> db.staff.find() [ { _id: ObjectId("6415df0b914137d28a613d08"), name: 'Trinh Quoc Viet', age: '18', gender: 'male', cert: [ 'AWS', 'Azure' ] }, { _id: ObjectId("6415df0b914137d28a613d09"), name: 'Nguyen Van Anh', age: '20', gender: 'female', cert: [ 'AWS', 'CKA', 'CKAD' ] }, { _id: ObjectId("6415e280914137d28a613d0b"), name: 'Nguyen Hoang Anh', age: '21', gender: 'male', cert: [ 'GCP', 'Azure' ] } ] demo> |
Using findOne()
Use the findOne()
function to select only one document. This function also accepts the query condition as an object and if left blank it will return the first document it finds.
1 2 3 4 5 6 7 8 9 | demo> db.staff.findOne() { _id: ObjectId("6415df0b914137d28a613d08"), name: 'Trinh Quoc Viet', age: '18', gender: 'male', cert: [ 'AWS', 'Azure' ] } |
Filter the data
We can filter data by parameters, for example filter which staff is name (condition is "gender": "male"
):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | demo> db.staff.find({"gender": "male"}) [ { _id: ObjectId("6415df0b914137d28a613d08"), name: 'Trinh Quoc Viet', age: '18', gender: 'male', cert: [ 'AWS', 'Azure' ] }, { _id: ObjectId("6415e280914137d28a613d0b"), name: 'Nguyen Hoang Anh', age: '21', gender: 'male', cert: [ 'GCP', 'Azure' ] } ] |
Filter field to get results by projection
We can filter out the required fields using the 2nd parameter in the find() function called projection . For example, we only want to get the name and cert information of male staff:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | demo> db.staff.find({"gender": "male"},{name:1 , cert: 1}) [ { _id: ObjectId("6415df0b914137d28a613d08"), name: 'Trinh Quoc Viet', cert: [ 'AWS', 'Azure' ] }, { _id: ObjectId("6415e280914137d28a613d0b"), name: 'Nguyen Hoang Anh', cert: [ 'GCP', 'Azure' ] } ] demo> |
This way can filter out unwanted fields, but the ” _id ” field, if you want to hide it, you have to set it to zero in the projection configuration:
1 2 3 4 5 6 7 | demo> db.staff.find({"gender": "male"},{_id: 0, name:1 , cert: 1}) [ { name: 'Trinh Quoc Viet', cert: [ 'AWS', 'Azure' ] }, { name: 'Nguyen Hoang Anh', cert: [ 'GCP', 'Azure' ] } ] demo> |
Update data in collection
To update data in the collection we can use updateOne() or updateMany() function. These functions take as the first parameter a query condition to choose which documents to update.
And the th parameter is an object that defines the data to be updated.
updateOne() function
The updateOne() function updates the first document that satisfies the query condition. For example, we need to update the age of male employees to 20:
Check data before updating:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | demo> db.staff.find({"gender": "male"}) [ { _id: ObjectId("6415df0b914137d28a613d08"), name: 'Trinh Quoc Viet', age: '18', gender: 'male', cert: [ 'AWS', 'Azure' ] }, { _id: ObjectId("6415e280914137d28a613d0b"), name: 'Nguyen Hoang Anh', age: '21', gender: 'male', cert: [ 'GCP', 'Azure' ] } ] |
Perform update
1 2 3 4 5 6 7 8 9 | demo> db.staff.updateOne({"gender": "male"}, {$set: { "age": 20 }} ) { acknowledged: true, insertedId: null, matchedCount: 1, modifiedCount: 1, upsertedCount: 0 } |
**After updating, only the first record found has been updated
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | demo> db.staff.find({"gender": "male"}) [ { _id: ObjectId("6415df0b914137d28a613d08"), name: 'Trinh Quoc Viet', age: 20, gender: 'male', cert: [ 'AWS', 'Azure' ] }, { _id: ObjectId("6415e280914137d28a613d0b"), name: 'Nguyen Hoang Anh', age: '21', gender: 'male', cert: [ 'GCP', 'Azure' ] } ] |
updateMany() function
This function will update all documents that satisfy the query condition. For example, we update all male employees to 25 years old:
1 2 3 4 5 6 7 8 9 | demo> db.staff.updateMany( {"gender": "male"}, {$set: { "age": 25 }} ) { acknowledged: true, insertedId: null, matchedCount: 2, modifiedCount: 2, upsertedCount: 0 } |
After updating, all records that satisfy the search criteria have been updated
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | demo> db.staff.find({"gender": "male"}) [ { _id: ObjectId("6415df0b914137d28a613d08"), name: 'Trinh Quoc Viet', age: 25, gender: 'male', cert: [ 'AWS', 'Azure' ] }, { _id: ObjectId("6415e280914137d28a613d0b"), name: 'Nguyen Hoang Anh', age: 25, gender: 'male', cert: [ 'GCP', 'Azure' ] } ] |
Delete data in the collection
Similar to the insert or update command, mongodb also has two delete functions, deleteOne() and deleteMany() . And still with the same idea, deleteOne() will delete the first document that meets the filter condition and deleteMany() will delete all documents that satisfy the filter condition.
Example delete female employee:
1 2 3 | demo> db.staff.deleteOne({"gender": "female"}) { acknowledged: true, deletedCount: 1 } |
Remove all male employees:
1 2 3 | demo> db.staff.deleteMany({"gender": "male"}) { acknowledged: true, deletedCount: 2 } |
Some operations with db and collection
List the collection in the db:
1 2 | db.getCollectionNames() |
Delete a collection:
1 2 3 | db.collection_name.drop() db.staff.drop() |
Delete a database:
1 2 3 | demo2> db.dropDatabase() { ok: 1, dropped: 'demo2' } |
Through this article, hopefully, those who are new to mongodb will be able to install a lab system and perform very basic operations with mongodb.
In the next article, I will show you how to install a mongodb sharded cluster with full components to ensure the elements of redundancy, high availability and scalable using sharding and replication.
If you find the article useful, please support by upvote the article to give me more motivation to write!
See you all in the next part!