What is MongoDB?
MongoDB is an open source database that uses a document-oriented data model. Instead of using tables and rows as in a relational database like MySQL, MongoDB uses collections and documents. Documents consist of key-value pairs and are the basic unit of data in MongoDB. Documents are grouped together in a collection which functions as the equivalent of relational database tables. Like other NoSQL databases, MongoDB supports dynamic schema design, allowing the documents in a collection to have different fields and structures.
Installation
In this tutorial I’m going to assume you have already installed MongoDB locally or have access to a working database server.
Connect to a database
From the system prompt start the mongo
shell by issuing the following command:
mongo
By default mongo
will connect to 127.0.0.1
on port 27017
. The prompt will look something like this after a successful connection:
MongoDB shell version: 2.4.8
connecting to: test
>
By default your session will connect to the database test
. (To report the name of the current database, issue the db
command.)
Select a database
In order to get a list of all available databases:
show dbs
For example, the server might contain these 3 databases:
local 0.078125GB
mydb 0.203125GB
test 0.203125GB
To switch to one of the databases:
use mydb
Create a database
You can use the same command to create a new database:
use newdb
This database will not yet show up if you execute the show dbs
command. But it will as soon as you add data to the database.
Insert data
Now it’s time to insert some data in our new database. First, create a couple of documents:
foo = { title : "playing with mongo" }
bar = { x : 42 }
These documents will be inserted into a new collection testData
by using the following commands:
db.testData.insert(foo)
db.testData.insert(bar)
The database and collection will be created as soon as the first command is executed.
To confirm that the collection indeed has been created, list all collections in the database:
show collections
which returns the collection testData
and the default collection system.indexes
.
Find data
Alright, we believe we have added 2 records into the database, but let’s confirm that they are really there by using the find
command:
db.testData.find()
This will return something like this:
{ "_id" : ObjectId("53fb83585e38bf930d1c9d3b"), "title" : "playing with mongo" }
{ "_id" : ObjectId("53fb835b5e38bf930d1c9d3c"), "x" : 42 }
And just as we expected, there are 2 records returned! As you can see, each of these records also has a _id
key with a unique value. This value is created by mongo
when you insert a new record.
In order to query for specific documents we pass an object to the find
command. For instance, to search for all documents with an x
key of 42
:
db.testData.find( { x :42 } )
which will return the only document that fits the search criteria:
{ "_id" : ObjectId("53fb835b5e38bf930d1c9d3c"), "x" : 42 }
To limit the number of documents in the result set by calling the limit()
method:
db.testData.find().limit(3)
to return the following set:
{ "_id" : ObjectId("53fb835b5e38bf930d1c9d3c"), "x" : 42 }
{ "_id" : ObjectId("53fb85995e38bf930d1c9d3d"), "x" : 137 }
{ "_id" : ObjectId("53fb859b5e38bf930d1c9d3e"), "x" : 273 }