MongoDB
MongoDB
MongoDB - Home
MongoDB - Overview
MongoDB - Advantages
MongoDB - Environment
MongoDB - Data Modeling
MongoDB - Create Database
MongoDB - Drop Database
MongoDB - Create Collection
MongoDB - Drop Collection
MongoDB - Data Types
MongoDB - Insert Document
MongoDB - Query Document
MongoDB - Update Document
MongoDB - Delete Document
MongoDB - Projection
MongoDB - Limiting Records
MongoDB - Sorting Records
MongoDB - Indexing
MongoDB - Aggregation
MongoDB - Replication
MongoDB - Sharding
MongoDB - Create Backup
MongoDB - Deployment
MongoDB - Java
MongoDB - PHP
Advanced MongoDB
MongoDB - Relationships
MongoDB - Database References
MongoDB - Covered Queries
MongoDB - Analyzing Queries
MongoDB - Atomic Operations
MongoDB - Advanced Indexing
MongoDB - Indexing Limitations
MongoDB - ObjectId
MongoDB - Map Reduce
MongoDB - Text Search
MongoDB - Regular Expression
Working with Rockmongo
MongoDB - GridFS
MongoDB - Capped Collections
Auto-Increment Sequence
MongoDB - Overview
MongoDB is a cross-platform, document oriented database that provides, high
performance, high availability, and easy scalability. MongoDB works on
concept of collection and document.
Database
Database is a physical container for collections. Each database gets its own
set of files on the file system. A single MongoDB server typically has multiple
databases.
Collection
Collection is a group of MongoDB documents. It is the equivalent of an
RDBMS table. A collection exists within a single database. Collections do not
enforce a schema. Documents within a collection can have different fields.
Typically, all documents in a collection are of similar or related purpose.
Document
A document is a set of key-value pairs. Documents have dynamic schema.
Dynamic schema means that documents in the same collection do not need to
have the same set of fields or structure, and common fields in a collection's
documents may hold different types of data.
The following table shows the relationship of RDBMS terminology with
MongoDB.
RDBMS MongoDB
Database Database
Table Collection
Tuple/Row Document
column Field
Mysqld/Oracle mongod
mysql/sqlplus mongo
Sample Document
Following example shows the document structure of a blog site, which is
simply a comma separated key value pair.
{
_id: ObjectId(7df78ad8902c)
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'Post para Programadores',
url: 'http://www.postparaprogramadores.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100,
comments: [
{
user:'user1',
message: 'My first comment',
dateCreated: new Date(2011,1,20,2,15),
like: 0
},
{
user:'user2',
message: 'My second comments',
dateCreated: new Date(2011,1,25,7,45),
like: 5
}
]
}
_id is a 12 bytes hexadecimal number which assures the uniqueness of every
document. You can provide _id while inserting the document. If you don’t
provide then MongoDB provides a unique id for every document. These 12
bytes first 4 bytes for the current timestamp, next 3 bytes for machine id, next 2
bytes for process id of MongoDB server and remaining 3 bytes are simple
incremental VALUE.
MongoDB - Environment
Let us now see how to install MongoDB on Windows.
Start MongoDB
sudo service mongodb start
Stop MongoDB
sudo service mongodb stop
Restart MongoDB
sudo service mongodb restart
To use MongoDB run the following command.
mongo
This will connect you to running MongoDB instance.
MongoDB Help
To get a list of commands, type db.help() in MongoDB client. This will give you
a list of commands as shown in the following screenshot.
MongoDB Statistics
To get stats about MongoDB server, type the command db.stats() in
MongoDB client. This will show the database name, number of collection and
documents in the database. Output of the command is shown in the following
screenshot.
Example
Suppose a client needs a database design for his blog/website and see the
differences between RDBMS and MongoDB schema design. Website has the
following requirements.
While in MongoDB schema, design will have one collection post and the
following structure −
{
_id: POST_ID
title: TITLE_OF_POST,
description: POST_DESCRIPTION,
by: POST_BY,
url: URL_OF_POST,
tags: [TAG1, TAG2, TAG3],
likes: TOTAL_LIKES,
comments: [
{
user:'COMMENT_BY',
message: TEXT,
dateCreated: DATE_TIME,
like: LIKES
},
{
user:'COMMENT_BY',
message: TEXT,
dateCreated: DATE_TIME,
like: LIKES
}
]
}
So while showing the data, in RDBMS you need to join three tables and in
MongoDB, data will be shown from one collection only.
Syntax
Example
In MongoDB default database is test. If you didn't create any database, then
collections will be stored in test database.
Syntax
Basic syntax of dropDatabase() command is as follows −
db.dropDatabase()
This will delete the selected database. If you have not selected any database,
then it will delete default 'test' database.
Example
First, check the list of available databases by using the command, show dbs.
>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
>
If you want to delete new database <mydb>, then dropDatabase() command
would be as follows −
>use mydb
switched to db mydb
>db.dropDatabase()
>{ "dropped" : "mydb", "ok" : 1 }
>
Now check list of databases.
>show dbs
local 0.78125GB
test 0.23012GB
>
Syntax
Options parameter is optional, so you need to specify only the name of the
collection. Following is the list of options you can use −
While inserting the document, MongoDB first checks size field of capped
collection, then it checks max field.
Examples
Syntax
Example
MongoDB - Datatypes
MongoDB supports many datatypes. Some of them are −
String − This is the most commonly used datatype to store the data. String in
MongoDB must be UTF-8 valid.
Integer − This type is used to store a numerical value. Integer can be 32 bit or 64
bit depending upon your server.
Boolean − This type is used to store a boolean (true/ false) value.
Double − This type is used to store floating point values.
Min/ Max keys − This type is used to compare a value against the lowest and
highest BSON elements.
Arrays − This type is used to store arrays or list or multiple values into one key.
Timestamp − ctimestamp. This can be handy for recording when a document has
been modified or added.
Object − This datatype is used for embedded documents.
Null − This type is used to store a Null value.
Symbol − This datatype is used identically to a string; however, it's generally
reserved for languages that use a specific symbol type.
Date − This datatype is used to store the current date or time in UNIX time format.
You can specify your own date time by creating object of Date and passing day,
month, year into it.
Object ID − This datatype is used to store the document’s ID.
Binary data − This datatype is used to store binary data.
Code − This datatype is used to store JavaScript code into the document.
Regular expression − This datatype is used to store regular expression.
Syntax
Example
>db.mycol.insert({
_id: ObjectId(7df78ad8902c),
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'Post para Programadores',
url: 'http://www.postparaprogramadores.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
})
Here mycol is our collection name, as created in the previous chapter. If the
collection doesn't exist in the database, then MongoDB will create this
collection and then insert a document into it.
In the inserted document, if we don't specify the _id parameter, then MongoDB
assigns a unique ObjectId for this document.
_id is 12 bytes hexadecimal number unique for every document in a collection.
12 bytes are divided as follows −
_id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes
process id,
3 bytes incrementer)
To insert multiple documents in a single query, you can pass an array of
documents in insert() command.
Example
>db.post.insert([
{
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'Post para Programadores',
url: 'http://www.postparaprogramadores.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
},
{
title: 'NoSQL Database',
description: "NoSQL database doesn't have tables",
by: 'Post para Programadores',
url: 'http://www.postparaprogramadores.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 20,
comments: [
{
user:'user1',
message: 'My first comment',
dateCreated: new Date(2013,11,10,2,35),
like: 0
}
]
}
])
To insert the document you can use db.post.save(document) also. If you
don't specify _id in the document then save() method will work same
as insert() method. If you specify _id then it will replace whole data of
document containing _id as specified in save() method.
Syntax
>db.mycol.find().pretty()
Example
>db.mycol.find().pretty()
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "Post para Programadores",
"url": "http://www.postparaprogramadores.com",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
>
Apart from find() method, there is findOne() method, that returns only one
document.
AND in MongoDB
Syntax
In the find() method, if you pass multiple keys by separating them by ',' then
MongoDB treats it as AND condition. Following is the basic syntax of AND −
>db.mycol.find(
{
$and: [
{key1: value1}, {key2:value2}
]
}
).pretty()
Example
Following example will show all the tutorials written by 'Post para
Programadores' and whose title is 'MongoDB Overview'.
>db.mycol.find({$and:[{"by":"Post para
Programadores"},{"title": "MongoDB Overview"}]}).pretty() {
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "Post para Programadores",
"url": "http://www.postparaprogramadores.com",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
For the above given example, equivalent where clause will be ' where by =
'Post para Programadores' AND title = 'MongoDB Overview' '. You can
pass any number of key, value pairs in find clause.
OR in MongoDB
Syntax
To query documents based on the OR condition, you need to use $or keyword.
Following is the basic syntax of OR −
>db.mycol.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()
Example
Following example will show all the tutorials written by 'Post para
Programadores' or whose title is 'MongoDB Overview'.
>db.mycol.find({$or:[{"by":"Post para
Programadores"},{"title": "MongoDB Overview"}]}).pretty()
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "Post para Programadores",
"url": "http://www.postparaprogramadores.com",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
>
The following example will show the documents that have likes greater than 10
and whose title is either 'MongoDB Overview' or by is 'Post para
Programadores'. Equivalent SQL where clause is 'where likes>10 AND (by =
'Post para Programadores' OR title = 'MongoDB Overview')'
>db.mycol.find({"likes": {$gt:10}, $or: [{"by": "Post para
Programadores"},
{"title": "MongoDB Overview"}]}).pretty()
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "Post para Programadores",
"url": "http://www.postparaprogramadores.com",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
>
Syntax
Example
Following example will set the new title 'New MongoDB Tutorial' of the
documents whose title is 'MongoDB Overview'.
>db.mycol.update({'title':'MongoDB
Overview'},{$set:{'title':'New MongoDB Tutorial'}})
>db.mycol.find()
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"New
MongoDB Tutorial"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL
Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Post para
Programadores Overview"}
>
By default, MongoDB will update only a single document. To update multiple
documents, you need to set a parameter 'multi' to true.
>db.mycol.update({'title':'MongoDB Overview'},
{$set:{'title':'New MongoDB Tutorial'}},{multi:true})
MongoDB Save() Method
The save() method replaces the existing document with the new document
passed in the save() method.
Syntax
Example
Syntax
Following example will remove all the documents whose title is 'MongoDB
Overview'.
>db.mycol.remove({'title':'MongoDB Overview'})
>db.mycol.find()
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL
Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Post para
Programadores Overview"}
>
MongoDB - Projection
In MongoDB, projection means selecting only the necessary data rather than
selecting whole of the data of a document. If a document has 5 fields and you
need to show only 3, then select only 3 fields from them.
Syntax
Example
Syntax
Example
Syntax
Example
Syntax
The basic syntax of sort() method is as follows −
>db.COLLECTION_NAME.find().sort({KEY:1})
Example
MongoDB - Indexing
Indexes support the efficient resolution of queries. Without indexes, MongoDB
must scan every document of a collection to select those documents that
match the query statement. This scan is highly inefficient and require
MongoDB to process a large volume of data.
Indexes are special data structures, that store a small portion of the data set in
an easy-to-traverse form. The index stores the value of a specific field or set of
fields, ordered by the value of the field as specified in the index.
Syntax
Example
>db.mycol.ensureIndex({"title":1})
>
In ensureIndex() method you can pass multiple fields, to create index on
multiple fields.
>db.mycol.ensureIndex({"title":1,"description":-1})
>
ensureIndex() method also accepts list of options (which are optional).
Following is the list −
MongoDB - Aggregation
Aggregations operations process data records and return computed results.
Aggregation operations group values from multiple documents together, and
can perform a variety of operations on the grouped data to return a single
result. In SQL count(*) and with group by is an equivalent of mongodb
aggregation.
Syntax
Example
Pipeline Concept
In UNIX command, shell pipeline means the possibility to execute an operation
on some input and use the output as the input for the next command and so
on. MongoDB also supports same concept in aggregation framework. There is
a set of possible stages and each of those is taken as a set of documents as
an input and produces a resulting set of documents (or the final resulting JSON
document at the end of the pipeline). This can then in turn be used for the next
stage and so on.
Following are the possible stages in aggregation framework −
$project − Used to select some specific fields from a collection.
$match − This is a filtering operation and thus this can reduce the amount of
documents that are given as input to the next stage.
$group − This does the actual aggregation as discussed above.
$sort − Sorts the documents.
$skip − With this, it is possible to skip forward in the list of documents for a given
amount of documents.
$limit − This limits the amount of documents to look at, by the given number
starting from the current positions.
$unwind − This is used to unwind document that are using arrays. When using an
array, the data is kind of pre-joined and this operation will be undone with this to
have individual documents again. Thus with this stage we will increase the amount
of documents for the next stage.
MongoDB - Replication
Replication is the process of synchronizing data across multiple servers.
Replication provides redundancy and increases data availability with multiple
copies of data on different database servers. Replication protects a database
from the loss of a single server. Replication also allows you to recover from
hardware failure and service interruptions. With additional copies of the data,
you can dedicate one to disaster recovery, reporting, or backup.
Why Replication?
To keep your data safe
High (24*7) availability of data
Disaster recovery
No downtime for maintenance (like backups, index rebuilds, compaction)
Read scaling (extra copies to read from)
Replica set is transparent to the application
Syntax
The basic syntax of rs.add() command is as follows −
>rs.add(HOST_NAME:PORT)
Example
You can add mongod instance to replica set only when you are connected to
primary node. To check whether you are connected to primary or not, issue the
command db.isMaster() in mongo client.
MongoDB - Sharding
Sharding is the process of storing data records across multiple machines and it
is MongoDB's approach to meeting the demands of data growth. As the size of
the data increases, a single machine may not be sufficient to store the data nor
provide an acceptable read and write throughput. Sharding solves the problem
with horizontal scaling. With sharding, you add more machines to support data
growth and the demands of read and write operations.
Why Sharding?
In replication, all writes go to master node
Latency sensitive queries still go to master
Single replica set has limitation of 12 nodes
Memory can't be large enough when active dataset is big
Local disk is not big enough
Vertical scaling is too expensive
Sharding in MongoDB
The following diagram shows the sharding in MongoDB using sharded cluster.
Syntax
Example
Start your mongod server. Assuming that your mongod server is running on the
localhost and port 27017, open a command prompt and go to the bin directory
of your mongodb instance and type the command mongodump
Consider the mycol collection has the following data.
>mongodump
The command will connect to the server running at 127.0.0.1 and
port 27017 and back all data of the server to directory /bin/dump/. Following is
the output of the command −
Restore data
To restore backup data MongoDB's mongorestore command is used. This
command restores all of the data from the backup directory.
Syntax
MongoDB - Deployment
When you are preparing a MongoDB deployment, you should try to understand
how your application is going to hold up in production. It’s a good idea to
develop a consistent, repeatable approach to managing your deployment
environment so that you can minimize any surprises once you’re in production.
The best approach incorporates prototyping your set up, conducting load
testing, monitoring key metrics, and using that information to scale your set up.
The key part of the approach is to proactively monitor your entire system - this
will help you understand how your production system will hold up before
deploying, and determine where you will need to add capacity. Having insight
into potential spikes in your memory usage, for example, could help put out a
write-lock fire before it starts.
To monitor your deployment, MongoDB provides some of the following
commands −
mongostat
This command checks the status of all running mongod instances and return
counters of database operations. These counters include inserts, queries,
updates, deletes, and cursors. Command also shows when you’re hitting page
faults, and showcase your lock percentage. This means that you're running low
on memory, hitting write capacity or have some performance issue.
To run the command, start your mongod instance. In another command
prompt, go to bin directory of your mongodb installation and type mongostat.
D:\set up\mongodb\bin>mongostat
Following is the output of the command −
mongotop
This command tracks and reports the read and write activity of MongoDB
instance on a collection basis. By default, mongotop returns information in
each second, which you can change it accordingly. You should check that this
read and write activity matches your application intention, and you’re not firing
too many writes to the database at a time, reading too frequently from a disk,
or are exceeding your working set size.
To run the command, start your mongod instance. In another command
prompt, go to bin directory of your mongodb installation and type mongotop.
D:\set up\mongodb\bin>mongotop
Following is the output of the command −
MongoDB - Java
In this chapter, we will learn how to set up MongoDB JDBC driver.
Installation
Before you start using MongoDB in your Java programs, you need to make
sure that you have MongoDB JDBC driver and Java set up on the machine.
You can check Java tutorial for Java installation on your machine. Now, let us
check how to set up MongoDB JDBC driver.
You need to download the jar from the path Download mongo.jar. Make sure to
download the latest release of it.
You need to include the mongo.jar into your classpath.
Connect to Database
To connect database, you need to specify the database name, if the database
doesn't exist then MongoDB creates it automatically.
Following is the code snippet to connect to the database −
import com.mongodb.client.MongoDatabase;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential =
MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database
successfully");
Now, let's compile and run the above program to create our database myDb as
shown below.
$javac ConnectToDB.java
$java ConnectToDB
On executing, the above program gives you the following output.
Connected to the database successfully
Credentials ::MongoCredential{
mechanism = null,
userName = 'sampleUser',
source = 'myDb',
password = <hidden>,
mechanismProperties = {}
}
Create a Collection
To create a collection, createCollection() method
of com.mongodb.client.MongoDatabase class is used.
Following is the code snippet to create a collection −
import com.mongodb.client.MongoDatabase;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential =
MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database
successfully");
//Creating a collection
database.createCollection("sampleCollection");
System.out.println("Collection created successfully");
}
}
On compiling, the above program gives you the following result −
Connected to the database successfully
Collection created successfully
Getting/Selecting a Collection
To get/select a collection from the database, getCollection() method
of com.mongodb.client.MongoDatabase class is used.
Following is the program to get/select a collection −
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential =
MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database
successfully");
// Creating a collection
System.out.println("Collection created successfully");
// Retieving a collection
MongoCollection<Document> collection =
database.getCollection("myCollection");
System.out.println("Collection myCollection selected
successfully");
}
}
Insert a Document
To insert a document into MongoDB, insert() method
of com.mongodb.client.MongoCollection class is used.
Following is the code snippet to insert a document −
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Retrieving a collection
MongoCollection<Document> collection =
database.getCollection("sampleCollection");
System.out.println("Collection sampleCollection
selected successfully");
import java.util.Iterator;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential =
MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database
successfully");
// Retrieving a collection
MongoCollection<Document> collection =
database.getCollection("sampleCollection");
System.out.println("Collection sampleCollection
selected successfully");
while (it.hasNext()) {
System.out.println(it.next());
i++;
}
}
}
Update Document
To update a document from the collection, updateOne() method
of com.mongodb.client.MongoCollection class is used.
Following is the program to select the first document −
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import java.util.Iterator;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential =
MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database
successfully");
// Retrieving a collection
MongoCollection<Document> collection =
database.getCollection("sampleCollection");
System.out.println("Collection myCollection selected
successfully");
collection.updateOne(Filters.eq("id", 1),
Updates.set("likes", 150));
System.out.println("Document update successfully...");
while (it.hasNext()) {
System.out.println(it.next());
i++;
}
}
}
On compiling, the above program gives you the following result −
Document update successfully...
Document {{
_id = 5967745223993a32646baab8,
title = MongoDB,
id = 1,
description = database,
likes = 150,
url = http://www.postparaprogramadores.com/mongodb/, by =
Post para Programadores
}}
Delete a Document
To delete a document from the collection, you need to use
the deleteOne() method of the com.mongodb.client.MongoCollection class.
Following is the program to delete a document −
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import java.util.Iterator;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential =
MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database
successfully");
// Retrieving a collection
MongoCollection<Document> collection =
database.getCollection("sampleCollection");
System.out.println("Collection sampleCollection
selected successfully");
while (it.hasNext()) {
System.out.println("Inserted Document: "+i);
System.out.println(it.next());
i++;
}
}
}
On compiling, the above program gives you the following result −
Connected to the database successfully
Collection sampleCollection selected successfully
Document deleted successfully...
Dropping a Collection
To drop a collection from a database, you need to use the drop() method of
the com.mongodb.client.MongoCollection class.
Following is the program to delete a collection −
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
public class DropingCollection {
// Creating Credentials
MongoCredential credential;
credential =
MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database
successfully");
// Creating a collection
System.out.println("Collections created successfully");
// Retieving a collection
MongoCollection<Document> collection =
database.getCollection("sampleCollection");
// Dropping a Collection
collection.drop();
System.out.println("Collection dropped successfully");
}
}
// Creating Credentials
MongoCredential credential;
credential =
MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
MongoDB - PHP
To use MongoDB with PHP, you need to use MongoDB PHP driver. Download
the driver from the url Download PHP Driver. Make sure to download the latest
release of it. Now unzip the archive and put php_mongo.dll in your PHP
extension directory ("ext" by default) and add the following line to your php.ini
file −
extension = php_mongo.dll
Create a Collection
Following is the code snippet to create a collection −
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->createCollection("mycol");
echo "Collection created succsessfully";
?>
Insert a Document
To insert a document into MongoDB, insert() method is used.
Following is the code snippet to insert a document −
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->mycol;
echo "Collection selected succsessfully";
$document = array(
"title" => "MongoDB",
"description" => "database",
"likes" => 100,
"url" =>
"http://www.postparaprogramadores.com/mongodb/",
"by" => "Post para Programadores"
);
$collection->insert($document);
echo "Document inserted successfully";
?>
// select a database
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->mycol;
echo "Collection selected succsessfully";
$cursor = $collection->find();
// iterate cursor to display title of documents
Update a Document
To update a document, you need to use the update() method.
In the following example, we will update the title of inserted document
to MongoDB Tutorial. Following is the code snippet to update a document −
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->mycol;
echo "Collection selected succsessfully";
Delete a Document
To delete a document, you need to use remove() method.
In the following example, we will remove the documents that has the
title MongoDB Tutorial. Following is the code snippet to delete a document −
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->mycol;
echo "Collection selected succsessfully";
MongoDB - Relationships
Relationships in MongoDB represent how various documents are logically
related to each other. Relationships can be modeled
via Embedded and Referenced approaches. Such relationships can be either
1:1, 1:N, N:1 or N:N.
Let us consider the case of storing addresses for users. So, one user can have
multiple addresses making this a 1:N relationship.
Following is the sample document structure of user document −
{
"_id":ObjectId("52ffc33cd85242f436000001"),
"name": "Tom Hanks",
"contact": "987654321",
"dob": "01-01-1991"
}
Following is the sample document structure of address document −
{
"_id":ObjectId("52ffc4a5d85242602e000000"),
"building": "22 A, Indiana Apt",
"pincode": 123456,
"city": "Los Angeles",
"state": "California"
}
This approach maintains all the related data in a single document, which
makes it easy to retrieve and maintain. The whole document can be retrieved
in a single query such as −
>db.users.findOne({"name":"Tom Benzamin"},{"address":1})
Note that in the above query, db and users are the database and collection
respectively.
The drawback is that if the embedded document keeps on growing too much in
size, it can impact the read/write performance.
Using DBRefs
There are three fields in DBRefs −
$ref − This field specifies the collection of the referenced document
$id − This field specifies the _id field of the referenced document
$db − This is an optional field and contains the name of the database in which the
referenced document lies
Consider a sample user document having DBRef field address as shown in
the code snippet −
{
"_id":ObjectId("53402597d852426020000002"),
"address": {
"$ref": "address_home",
"$id": ObjectId("534009e4d852427820000002"),
"$db": "postparaprogramadores"},
"contact": "987654321",
"dob": "01-01-1991",
"name": "Tom Benzamin"
}
The address DBRef field here specifies that the referenced address document
lies in address_home collection under postparaprogramadores database
and has an id of 534009e4d852427820000002.
The following code dynamically looks in the collection specified
by $ref parameter (address_home in our case) for a document with id as
specified by $id parameter in DBRef.
>var user = db.users.findOne({"name":"Tom Benzamin"})
>var dbRef = user.address
>db[dbRef.$ref].findOne({"_id":(dbRef.$id)})
Using $explain
The $explain operator provides information on the query, indexes used in a
query and other statistics. It is very useful when analyzing how well your
indexes are optimized.
In the last chapter, we had already created an index for the users collection on
fields gender and user_name using the following query −
>db.users.ensureIndex({gender:1,user_name:1})
We will now use $explain on the following query −
>db.users.find({gender:"M"},{user_name:1,_id:0}).explain()
The above explain() query returns the following analyzed result −
{
"cursor" : "BtreeCursor gender_1_user_name_1",
"isMultiKey" : false,
"n" : 1,
"nscannedObjects" : 0,
"nscanned" : 1,
"nscannedObjectsAllPlans" : 0,
"nscannedAllPlans" : 1,
"scanAndOrder" : false,
"indexOnly" : true,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"gender" : [
[
"M",
"M"
]
],
"user_name" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
]
}
}
We will now look at the fields in this result set −
The true value of indexOnly indicates that this query has used indexing.
The cursor field specifies the type of cursor used. BTreeCursor type indicates that
an index was used and also gives the name of the index used. BasicCursor
indicates that a full scan was made without using any indexes.
n indicates the number of documents matching returned.
nscannedObjects indicates the total number of documents scanned.
nscanned indicates the total number of documents or index entries scanned.
Using $hint
The $hint operator forces the query optimizer to use the specified index to run
a query. This is particularly useful when you want to test performance of a
query with different indexes. For example, the following query specifies the
index on fields gender and user_name to be used for this query −
>db.users.find({gender:"M"},{user_name:1,_id:0}).hint({gender
:1,user_name:1})
Once the index is created, we can search for any of the sub-document fields
utilizing this index as follows −
>db.users.find({"address.city":"Los Angeles"})
Remember that the query expression has to follow the order of the index
specified. So the index created above would support the following queries −
>db.users.find({"address.city":"Los
Angeles","address.state":"California"})
It will also support the following query −
>db.users.find({"address.city":"LosAngeles","address.state":"
California",
"address.pincode":"123"})
Extra Overhead
Every index occupies some space as well as causes an overhead on each
insert, update and delete. So if you rarely use your collection for read
operations, it makes sense not to use indexes.
RAM Usage
Since indexes are stored in RAM, you should make sure that the total size of
the index does not exceed the RAM limit. If the total size increases the RAM
size, it will start deleting some indexes, causing performance loss.
Query Limitations
Indexing can't be used in queries which use −
Maximum Ranges
A collection cannot have more than 64 indexes.
The length of the index name cannot be longer than 125 characters.
A compound index can have maximum 31 fields indexed.
MongoDB - ObjectId
We have been using MongoDB Object Id in all the previous chapters. In this
chapter, we will understand the structure of ObjectId.
An ObjectId is a 12-byte BSON type having the following structure −
The first 4 bytes representing the seconds since the unix epoch
The next 3 bytes are the machine identifier
The next 2 bytes consists of process id
The last 3 bytes are a random counter value
MongoDB uses ObjectIds as the default value of _id field of each document,
which is generated while the creation of any document. The complex
combination of ObjectId makes all the _id fields unique.
Creating New ObjectId
To generate a new ObjectId use the following code −
>newObjectId = ObjectId()
The above statement returned the following uniquely generated id −
ObjectId("5349b4ddd2781d08c09890f3")
Instead of MongoDB generating the ObjectId, you can also provide a 12-byte id
−
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
MapReduce Command
Following is the syntax of the basic mapReduce command −
>db.collection.mapReduce(
function() {emit(key,value);}, //map function
function(key,values) {return reduceFunction}, { //reduce
function
out: collection,
query: document,
sort: document,
limit: number
}
)
The map-reduce function first queries the collection, then maps the result
documents to emit key-value pairs, which is then reduced based on the keys
that have multiple values.
In the above syntax −
map is a javascript function that maps a value with a key and emits a key-value
pair
reduce is a javascript function that reduces or groups all the documents having the
same key
out specifies the location of the map-reduce query result
query specifies the optional selection criteria for selecting documents
sort specifies the optional sort criteria
limit specifies the optional maximum number of documents to be returned
Using MapReduce
Consider the following document structure storing user posts. The document
stores user_name of the user and the status of post.
{
"post_text": "postparaprogramadores is an awesome website
for tutorials",
"user_name": "mark",
"status":"active"
}
Now, we will use a mapReduce function on our posts collection to select all
the active posts, group them on the basis of user_name and then count the
number of posts by each user using the following code −
>db.posts.mapReduce(
function() { emit(this.user_id,1); },
).find()
The above query gives the following result which indicates that both
users tom and mark have two posts in active states −
{ "_id" : "tom", "value" : 2 }
{ "_id" : "mark", "value" : 2 }
One of the results returned from this query is the following document which
contains the word postparaprogramadores in different cases −
{
"_id" : ObjectId("53493d37d852429c10000004"),
"post_text" : "hey! this is my post on
Postparaprogramadores",
"tags" : [ "postparaprogramadores" ]
}
Downloading RockMongo
You can download the latest version of RockMongo from
here: https://github.com/iwind/rockmongo
Installing RockMongo
Once downloaded, you can unzip the package in your server root folder and
rename the extracted folder to rockmongo. Open any web browser and
access the index.php page from the folder rockmongo. Enter admin/admin as
username/password respectively.
Export/Import Data
To import/export data of any collection, click on that collection and then click
on Export/Import link on the top panel. Follow the next instructions to export
your data in a zip format and then import the same zip file to import back data.
MongoDB - GridFS
GridFS is the MongoDB specification for storing and retrieving large files such
as images, audio files, video files, etc. It is kind of a file system to store files but
its data is stored within MongoDB collections. GridFS has the capability to
store files even greater than its document size limit of 16MB.
GridFS divides a file into chunks and stores each chunk of data in a separate
document, each of maximum size 255k.
GridFS by default uses two collections fs.files and fs.chunks to store the file's
metadata and the chunks. Each chunk is identified by its unique _id ObjectId
field. The fs.files serves as a parent document. The files_id field in the
fs.chunks document links the chunk to its parent.
Following is a sample document of fs.files collection −
{
"filename": "test.txt",
"chunkSize": NumberInt(261120),
"uploadDate": ISODate("2014-04-13T11:32:33.557Z"),
"md5": "7b762939321e146569b07f72c62cca4f",
"length": NumberInt(646)
}
The document specifies the file name, chunk size, uploaded date, and length.
Following is a sample document of fs.chunks document −
{
"files_id": ObjectId("534a75d19f54bfec8a2fe44b"),
"n": NumberInt(0),
"data": "Mongo Binary Data"
}
We can also see all the chunks present in fs.chunks collection related to the
stored file with the following code, using the document id returned in the
previous query −
>db.fs.chunks.find({files_id:ObjectId('534a811bf8b4aa4d33fdf9
4d')})
In my case, the query returned 40 documents meaning that the whole mp3
document was divided in 40 chunks of data.
In addition to collection size, we can also limit the number of documents in the
collection using the max parameter −
>db.createCollection("cappedLogCollection",{capped:true,size:
10000,max:1000})
return sequenceDocument.sequence_value;
}
>db.products.insert({
"_id":getNextSequenceValue("productid"),
"product_name":"Samsung S3",
"category":"mobiles"
})