In this recipe, we will look at basic CRUD operations with MongoDB. We will learn how to create databases, store, retrieve, and update stored data. This is a recipe to get started with MongoDB.
Make sure that you have installed and configured MongoDB. You can also use the MongoDB installation on a remote server.
Follow these steps to store and retrieve data with MongoDB:
$ mongo
server_ip
and port
with the respective values:$ mongo server_ip:port/db
use dbname
. Since schemas in MongoDB are dynamic, you do not need to create a database before using it:> use testdb
help
in Mongo shell to get a list of available commands and help regarding a specific command:
> help
: Let’s insert our first document:
> db.users.insert({‘name’:’ubuntu’,’uid’:1001})
> show dbs
> show collections
> db.users.insert({‘name’:’root’,’uid’:1010, ‘gid’:[1010, 1000, 1111]})
> db.users.find()
findOne()
:> db.users.findOne({uid:1010})
update
command as follows:> db.users.update({name:’ubuntu’}, {$set:{uid:2222}})
remove
command. This will remove all records with a name
equal to ubuntu
:> db.users.remove({‘name’:’ubuntu’})
drop()
command:> db.users.drop()
dropDatabase()
command:> db.users.dropDatabase()
The preceding examples show very basic CRUD operations with the MongoDB shell interface. MongoDB shell is also a JavaScript shell. You can execute all JS commands in a MongoDB shell. You can also modify the shell with the configuration file, ~/.mongorc.js
. Similar to shell, MongoDB provides language-specific drivers, for example, MongoDB PHP drivers to access MongoDB from PHP.
MongoDB works on the concept of collections and documents. A collection is similar to a table in MySQL and a document is a set of key value stores where a key is similar to a column in a MySQL table. MongoDB does not require any schema definitions and accepts any pair of keys and values in a document. Schemas are dynamically created. In addition, you do not need to explicitly create the collection. Simply type a collection name in a command and it will be created if it does not already exist. In the preceding example, users
is a collection we used to store all data. To explicitly create a collection, use the following command:
> use testdb > db.createCollection(‘users’)
You may be missing the where
clause in MySQL queries. We have already used that with the findOne()
command:
> db.users.findOne({uid:1010})
You can use $lt
for less than, $lte
for less than or equal to, $gt
for greater than, $gte
for greater than or equal to, and $ne
for not equal:
> db.users.findOne({uid:{$gt:1000}})
In the preceding example, we have used the where
clause with the equality condition uid=1010
. You can add one more condition as follows:
> db.users.findOne({uid:1010, name:’root’})
To use the or
condition, you need to modify the command as follows:
> db.users.find ({$or:[{name:’ubuntu’}, {name:’root’}]})
You can also extract a single key (column) from the entire document. The find
command accepts a second optional parameter where you can specify a select criteria. You can use values 1
or 0
. Use 1
to extract a specific key and 0
otherwise:
> db.users.findOne({uid:1010}, {name:1})
> db.users.findOne({uid:1010}, {name:0})
You can install a web interface to manage the MongoDB installation. There are various open source web interfaces listed on Mongo documentation at http://docs.mongodb.org/ecosystem/tools/administration-interfaces/.
When you start a mongo shell for the first time, you may see a warning message regarding transperent_hugepage
and defrag. To remove those warnings, add the following lines to /etc/init/mongod.conf
, below the $DAEMONUSER /var/run/mongodb.pid
line:
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then echo never > /sys/kernel/mm/transparent_hugepage/enabled fi if test -f /sys/kernel/mm/transparent_hugepage/defrag; then echo never > /sys/kernel/mm/transparent_hugepage/defrag fi
Find more details on this Stack Overflow post at http://stackoverflow.com/questions/28911634/how-to-avoid-transparent-hugepage-defrag-warning-from-mongodb