This document provides an introduction and overview of MongoDB including what MongoDB is, how to install and configure it, common database commands, and CRUD operations. MongoDB is an open-source document database that provides high performance and automatic scaling. Records in MongoDB are documents composed of field-value pairs similar to JSON objects. The document covers installing MongoDB, starting the MongoDB service, basic CRUD commands like insert, find, update, and remove, and provides examples of each.
5. Not Only SQL Database.
A form of Data Base Managment System which is non-
relational.
Systems are often schema less, avoid joins and easy to scale.
Term NoSQL was coined in 1998, by Carlo Strozzi and again in
early 2009 with no:sql east confrence.
The better term would have been “No REL”, but NoSQL
caught on.
Redis, MongoDB, CouchDB, are type of NoSQL
9. MongoDB is an open source document database that provides high
performance, high availability and automatic scaling.
A record in MongoDB is a document, which is a data structure
composed of fields and value pairs.
MongoDB objects are very similar to JSON Objects.
{
Name:”vijay”,
Age:23,
}
11. 1. Import public key used by package management system
a. sudo apt-key adv --keyserver
hkp://keyserver.ubuntu.com:80 --recv EA312927
1. Create a list file for MongoDB
a. echo "deb http://repo.mongodb.org/apt/ubuntu
precise/mongodb-org/3.2 multiverse" | sudo tee
/etc/apt/sources.list.d/mongodb-org-3.2.list
1. Reload local package database
a. sudo apt-get update
1. Install MongoDB packages
a. sudo apt-get install -y mongodb-org
1. Install a specific release
a. sudo apt-get install -y mongodb-org=3.2.4 mongodb-org-
server=3.2.4 mongodb-org-shell=3.2.4 mongodb-org-
mongos=3.2.4 mongodb-org-tools=3.2.4
Installation Guide
12. Starting MongoDB Service
Mongod
Once we have issue this command, MongoDB will start
up and you should see:
waiting for connections on port 27017
Connecting to Mongodb Service
mongo
13. However, after all the steps above and mongodb installed in our
machine, now if you try to start the mongodb service sudo service
mongod start it will fail.
"start: Unable to connect to Upstart: Failed to connect to socket
/com/ubuntu/upstart: Connection refused”
Failed to start mongod.service: Unit mongod.service failed to load:
No such file or directory.
You probably will be able to manually start it with sudo mongod -f
/etc/mongod.conf - but in this way MongoDB will remain connected as long as that
terminal is opened.
Configuration Guide
14. 1. First we are going to change MongoDB’s default data store files from
/var/lib/mongodb to/data/db
1. So, first create a folder /data/db in your machine. Run sudo mkdir -p
/data/db
1. Now open the main mongo configuration file with sudo gedit
/etc/mongod.conf and change the “dbpath” line as below
1. Replace dbpath=/var/lib/mongodb TO dbpath=/data/db and then save
the file.
1. Then delete the old default /var/lib/mongodb
1. But the directory you created doesn’t have the correct permissions and
ownership right after creation – it needs to be writable by the user who
runs the MongoDB process. Hence we must make all the directories/files
owned by mongod user
1. Run sudo chown -R mongodb:mongodb /data/db
Configuration Guide
17. Basic Commands
show dbs List All Databases
use db_name Change DB or Create DB
db Show current selected database name
save Create both the database and collection
find Read
update Update
remove Delete
19. // save one user
$ db.users.save({ name: 'Chris' });
// save multiple users
$ db.users.save([{ name: 'Chris'}, { name: 'Holly' }]);
Create
Read
// show all users
$ db.users.find();
// find a specific user
$ db.users.find({ name: 'Holly' });