Вы находитесь на странице: 1из 188

UNIT II

Architecture with NoSQL


In order to understand how to properly
architect applications with NoSQL
databases you must understand the
separation of concerns between data
management and data storage. The past
era of SQL based databases attempted to
satisfy both concerns with databases. This
is very difficult, and inevitably applications
would take on part of the task of data
management, providing certain validation
tasks and adding modeling logic. One of the
key concepts of the NoSQL movement is to
have DBs focus on the task of
high-performance scalable data storage,
and provide low-level access to a data
management layer in a way that allows data
management tasks to be conveniently
written in the programming language of
choice rather than having data
management logic spread across
Turing-complete application languages,
SQL, and sometimes even DB-specific
stored procedure languages.
Complex Data Structures

One important capability that most NoSQL databases provide is hierarchical


nested structures in data entities. Hierarchical data and data with list type
structures are easily described with JSON and other formats used by NoSQL
databases, where multiple tables with relations would be necessary in
traditional SQL databases to describe these data structures. Furthermore,
JSON (or alternatives) provide a format that much more closely matches the
common programming languages data structure, greatly simplifying object
mapping. The ability to easily store object-style structures without impedance
mismatch is a big attractant of NoSQL.

Nested data structures work elegantly in situations where the


children/substructures are always accessed from within a parent document.
Object oriented and RDF databases also work well with data structures that
are uni-directional, one object is accessed from another, but not vice versa.
However, if the data entities may need to be individually accessed and
updated or relations are bi-directional, real relations become necessary. For
example, if we had a database of employees and employers, we could easily
envision scenarios where we would start with an employee and want to find
their employer, or start with an employer and find all their employees. It may
also be desirable to individually update an employee or employer without
having to worry about updating all the related entities.

In some situations, nested structures can eliminate unnecessary bi-directional


relations and greatly simplify database design, but there are still critical parts
of real applications where relations are essential.

Handling Relational Data

The NoSQL style databases has often been termed non-relational databases.
This is an unfortunate term. These databases can certainly be used with data
that has relations, which is actually extremely important. In fact, real data
almost always has relations. Truly non-relational data management would be
virtually worthless. Understanding how to deal with relations has not always
been well-addressed by NoSQL discussions and is perhaps one of the most
important issues for real application development on top of NoSQL
databases.

The handling of relations with traditional RDBMSs is very well understood.


Table structures are defined by data normalization, and data is retrieved
through SQL queries that often make extensive use of joins to leverage the
relations of data to aggregate information from multiple normalized tables.
The benefits of normalization are also clear. How then do we model relations
and utilize them with NoSQL databases?

There are a couple approaches. First, we can retain normalization strategies


and avoid any duplication of data. Alternately, we can choose to de-normalize
data which can have benefits for improved query performance.

With normalized data we can preserve key invariants, making it easy to


maintain consistent data, without having to worry about keeping duplicated
data in sync. However, normalization can often push the burden of effort on to
queries to aggregate information from multiple records and can often incur
substantial performance costs. Substantial effort has been put into providing
high-performance JOINs in RDBMSs to provide optimally efficient access to
normalized data. However, in the NoSQL world, most DBs do not provide any
ad-hoc JOIN type of query functionality. Consequently, to perform a query that
aggregates information across tables often requires application level iteration,
or creative use of map-reduce functions. Queries that utilize joining for
filtering across different mutable records often cannot be properly addressed
with map-reduce functions, and must use application level iteration.

NoSQL advocates might suggest that the lack of JOIN functionality is


beneficial; it encourages de-normalization that provides much more efficient
query-time data access. All aggregation happens for each (less frequent)
write, thus allowing queries to avoid any O(n) aggregation operations.
However, de-normalization can have serious consequences. De-normalization
means that data is prone to inconsistencies. Generally, this means duplication
of data; when that data is mutated, applications must rely on synchronization
techniques to avoid having copies become inconsistent. This invariant can
easily be violated by application code. While it is typically suitable for multiple
applications to access database management servers, with de-normalized
data, database access becomes fraught with invariants that must be carefully
understood.

These hazards do not negate the value of database de-normalization as an


optimization and scalability technique. However, with such an approach,
database access should be viewed as an internal aspect of implementation
rather than a reusable API. The management of data consistency becomes an
integral compliment to the NoSQL storage as part of the whole database
system.

The NoSQL approach is headed in the wrong direction if it is attempting to


invalidate the historic pillars of data management, established by Edgar Codd.
These basic rules for maintaining consistent data are timeless, but with the
proper architecture a full NoSQL-based data management system does not
need to contradict these ideas. Rather it couples NoSQL data storage engines
with database management logic, allowing for these rules to be fulfilled in
much more natural ways. In fact, Codd himself, the undisputed father of
relational databases, was opposed to SQL. Most likely, he would find a
properly architected database management application layer combined with a
NoSQL storage engine to fit much closer to his ideals of a relational database
then the traditional SQL database.

Network or In-process Programmatic Interaction?

With the vastly different approach of NoSQL servers, it is worth considering if


the traditional network-based out-of-process interaction approach of SQL
servers is truly optimal for NoSQL servers. Interestingly, both of the
approaches to relational data point to the value of more direct in-process
programmatic access to indexes rather than the traditional
query-request-over-tcp style communication. JOIN style queries over
normalized data is very doable with NoSQL databases, but it relies on iterating
through data sets with lookups during each loop. These lookups can be very
cheap at the index level, but can incur a lot of overhead at the TCP handling
and query parsing level. Direct programmatic interaction with the database
sidesteps the unnecessary overhead, allowing for reasonably fast ad-hoc
relational queries. This does not hinder clustering or replication across
multiple machines, the data management layer can be connected to the
storage system on each box.

De-normalization approaches also work well with in-process programmatic


access. Here the reasons are different. Now, access to the database should
be funneled through a programmatic layer that handles all data
synchronization needs to preserve invariants so that multiple higher level
application modules can safely interact with the database(whether
programmatically or a higher level TCP/IP based communication such as
HTTP). With programmatic-only access, the data can be more safely
protected from access that might violate integrity expectations.

Browser vendors have also come to similar conclusions of programmatic


access to indexes rather than query-based access in the W3C process to
define the browser-based database API. Earlier efforts to provide
browser-based databases spurred by Google Gears and later implemented in
Safari were SQL-based. But the obvious growing dissatisfaction with SQL
among developers and the impedance mismatches between RDBMS style
data structures and JavaScript style data structures, has led the W3C, with a
proposal from Oracle (and supported by Mozilla and Microsoft), to orient
towards a NoSQL-style indexed key-value document database API modeled
after the Berkeley DB API.

Schemas/Validation

Most NoSQL databases could also be called schema-free databases as this is


often one of the most highly touted aspects of these type of databases. The
key advantage of schema-free design is that it allows applications to quickly
upgrade the structure of data without expensive table rewrites. It also allows
for greater flexibility in storing heterogeneously structured data. But while
applications may benefit greatly from freedom from storage schemas, this
certainly does not eliminate the need to enforce data validity and integrity
constraints.

Moving the validity/integrity enforcement to the data management layer has


significant advantages. SQL databases had very limited stiff schemas,
whereas we have much more flexibility enforcing constraints with a
programming language. We can enforce complex rules, mix strict type
enforcements on certain properties, and leave other properties free to carry
various types or be optional. Validation can even employ access to external
systems to verify data. By moving validation out of the storage layer, we can
centralize validation in our data management layer and have the freedom to
create rich data structures and evolve our applications without storage
system induced limitations.

ACID/BASE and Relaxing Consistency Constraints

One aspect of the NoSQL movement has been a move away from trying to
maintain completely perfect consistency across distributed servers (everyone
has the same view of data) due to the burden this places on databases,
particularly in distributed systems. The now famous CAP theorem states that
of consistency, availability, and network partitioning, only two can be
guaranteed at any time. Traditional relational databases have kept strict
transactional semantics to preserve consistency, but many NoSQL databases
are moving towards a more scalable architecture that relaxes consistency.
Relaxing consistency is often called eventual consistency. This permits much
more scalable distributed storage systems where writes can occur without
using two phase commits or system-wide locks.

However, relaxing consistency does lead to the possibility of conflicting


writes. When multiple nodes can accept modifications without expensive lock
coordination, concurrent writes can occur in conflict. Databases like CouchDB
will put objects into a conflict state when this occurs. However, it is inevitably
the responsibility of the application to deal with these conflicts. Again, our
suggested data management layer is naturally the place for the conflict
resolution logic.

Data management can also be used to customize the consistency level. In


general, one can implement more relaxed consistency-based replication
systems on top of individual database storage systems based on stricter
transactional semantics. Customized replication and consistency
enforcements can be very useful for applications where some updates may
require higher integrity and some may require the higher scalability of relaxed
consistency.

Customizing replication can also be useful for determining exactly what


constitutes a conflict. Multi-Version Concurency Control (MVCC) style conflict
resolution like that of CouchDB can be very naive. MVCC assumes the
precondition for any update is the version number of the previous version of
the document. This certainly is not necessarily always the correct
precondition, and many times unexpected inconsistent data may be due to
updates that were based on other record/document states. Creating the
proper update logs and correctly finding conflicts during synchronization can
often involve application level design decisions that a storage can’t make on
its own.

Persevere

Persevere is a RESTful persistence framework, version 2.0 is designed for


NoSQL databases while maintaining the architecture principles of the
relational model, providing a solid complementary approach. Persevere’s
persistence system, Perstore, uses a data store API that is actually directly
modeled after W3C’s No-SQL-inspired indexed database API. Combined with
Persevere’s RESTful HTTP handler (called Pintura), data can be efficiently and
scalably stored in NoSQL storage engines and accessed through a data
modeling logic layer that allows users to access data in familiar RESTful
terms with appropriate data views, while preserving data consistency.
Persevere provides JSON Schema based validation, data modification,
creation, and deletion handlers, notifications, and a faceted-based security
system.
Performing CRUD operations
CRUD operations create, read, update,
and delete documents.

Create Operations
Create or insert operations add
new documents to a collection. If the
collection does not currently exist, insert
operations will create the collection.

MongoDB provides the following methods


to insert documents into a collection:
∙ db.collection.insertOne() 
∙ db.collection.insertMany() 
In MongoDB, insert operations target a
single collection. All write operations in
MongoDB are atomic on the level of a
single document.

For examples, see Insert Documents.

This page provides examples of insert


operations in MongoDB.
CREATING A COLLECTION
If the collection does not currently exist,
insert operations will create the collection.

Insert a Single Document

db.collection.insertOne() inserts
a single document into a collection.
The following example inserts a new
document into the inventory collection. If
the document does not specify an _id field,
MongoDB adds the _id field with an
ObjectId value to the new document.
See Insert Behavior.
copy
copied
db.inventory.insertOne(
{ item: "canvas", qty: 100, tags: ["cotton"], size: { h:
28, w: 35.5, uom: "cm" } }
)

You can run the operation in the web shell


below:

insertOne() returns a document that


includes the newly inserted
document’s _id field value. For an example
of a return document,
see db.collection.insertOne() reference.
To retrieve the document that you just
inserted, query the collection:
copy
copied
db.inventory.find( { item: "canvas" } )

Insert Multiple Documents


New in version 3.2.
db.collection.insertMany() can
insert multiple documents into a collection.
Pass an array of documents to the method.

The following example inserts three new


documents into the inventory collection. If
the documents do not specify an _id field,
MongoDB adds the _id field with an
ObjectId value to each document.
See Insert Behavior.
copy
copied
db.inventory.insertMany([
{ item: "journal", qty: 25, tags: ["blank", "red"], size: {
h: 14, w: 21, uom: "cm" } },
{ item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w:
35.5, uom: "cm" } },
{ item: "mousepad", qty: 25, tags: ["gel", "blue"], siz
e: { h: 19, w: 22.85, uom: "cm" } }
])

Read Operations
Read operations retrieves documents from
a collection; i.e. queries a collection for
documents. MongoDB provides the
following methods to read documents from
a collection:
∙ db.collection.find()

You can specify query filters or criteria that


identify the documents to return.

This page provides examples of query


operations using
the db.collection.find() method in
the mongoshell. The examples on this page
use the inventory collection. To populate
the inventory collection, run the following:
copy
copied
db.inventory.insertMany([
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "c
m" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom:
"in" }, status: "A" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "i
n" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uo
m: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uo
m: "cm" }, status: "A" }
]);

You can run the operation in the web shell


below:

Select All Documents in a


Collection
To select all documents in the collection,
pass an empty document as the query filter
parameter to the find method. The query
filter parameter determines the select
criteria:
copy
copied
db.inventory.find( {} )
This operation corresponds to the following
SQL statement:
copy
copied
SELECT * FROM inventory

For more information on the syntax of the


method, see find().

Specify Equality Condition


To specify equality conditions,
use <field>:<value> expressions in the query
filter document:

{ <field1>: <value1>, ... }

The following example selects from


the inventory collection all documents
where the status equals "D":
db.inventory.find( { status: "D" } )

This operation corresponds to the following


SQL statement:
SELECT * FROM inventory WHERE status = "D"

Specify Conditions Using

Query Operators
A query filter document can use the query
operators to specify conditions in the
following form:
{ <field1>: { <operator1>: <value1> }, ... }

The following example retrieves all


documents from the inventory collection
where status equals either "A"or "D":
db.inventory.find( { status: { $in: [ "A", "D" ] } } )
NOTE
Although you can express this query using
the $or operator, use the $in operator rather
than the $oroperator when performing
equality checks on the same field.

The operation corresponds to the following


SQL statement:
copy
copied
SELECT * FROM inventory WHERE status in ("A", "D")

Refer to the Query and Projection


Operators document for the complete list of
MongoDB query operators.

Specify AND Conditions
A compound query can specify conditions
for more than one field in the collection’s
documents. Implicitly, a
logical AND conjunction connects the
clauses of a compound query so that the
query selects the documents in the
collection that match all the conditions.

The following example retrieves all


documents in the inventory collection
where the status equals "A" andqty is less
than ($lt) 30:
db.inventory.find( { status: "A", qty: { $lt: 30 } } )

The operation corresponds to the following


SQL statement:
SELECT * FROM inventory WHERE status = "A" AND
qty < 30

See comparison operators for other


MongoDB comparison operators.
Specify OR Conditions
Using the $or operator, you can specify a compound query that
joins each clause with a logical OR conjunction so that the query
selects the documents in the collection that match at least one
condition.

The following example retrieves all documents in the collection


where the status equals "A" or qty is less than ($lt) 30:

copy

copied
db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30
}}]})
The operation corresponds to the following SQL statement:

copy

copied

SELECT * FROM inventory WHERE status = "A" OR qty < 30


NOTE
Queries which use comparison
operators are subject to Type Bracketing.

Specify AND as well as OR Conditions

In the following example, the compound


query document selects all documents in
the collection where
the status equals "A" and either qty is less
than ($lt) 30 or item starts with the
character p:
db.inventory.find( {
status: "A",
$or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]
})

The operation corresponds to the following


SQL statement:
SELECT * FROM inventory WHERE status = "A" AND
( qty < 30 OR item LIKE "p%")

∙ Query on Embedded/Nested Documents

∙ This page provides examples of query


operations on embedded/nested
documents using the

∙ db.collection.find() method in
the mongo shell. The examples on this
page use the inventorycollection. To
populate the inventory collection, run the
following:
∙ copy

∙ copied

∙ db.inventory.insertMany( [
∙ { item: "journal", qty: 25, size: { h: 14, w: 21, uo
m: "cm" }, status: "A" },
∙ { item: "notebook", qty: 50, size: { h: 8.5, w: 11,
uom: "in" }, status: "A" },
∙ { item: "paper", qty: 100, size: { h: 8.5, w: 11, uo
m: "in" }, status: "D" },
∙ { item: "planner", qty: 75, size: { h: 22.85, w: 30,
uom: "cm" }, status: "D" },
∙ { item: "postcard", qty: 45, size: { h: 10, w: 15.2
5, uom: "cm" }, status: "A" }
∙ ]);

∙ You can run the operation in the web


shell below:

∙ Match an
Embedded/Nested
Document
∙ To specify an equality condition on a
field that is an embedded/nested
document, use the query filter
document {<field>: <value> } where <valu
e> is the document to match.

∙ For example, the following query selects


all documents where the
field size equals the
document { h: 14, w:21, uom: "cm" }:
∙ copy

∙ copied

∙ db.inventory.find( { size: { h: 14, w: 21, uom: "cm"


}})

∙ Equality matches on the whole


embedded document require
an exact match of the
specified <value> document, including
the field order. For example, the
following query does not match any
documents in the inventorycollection:
∙ copy

∙ copied

∙ db.inventory.find( { size: { w: 21, h: 14, uom: "cm


"}} )

∙ Query on Nested Field


∙ To specify a query condition on fields in
an embedded/nested document, use dot
notation("field.nestedField").
∙ NOTE
∙ When querying using dot notation, the
field and nested field must be inside
quotation marks.
∙ Specify Equality Match on a Nested Field

∙ The following example selects all


documents where the field uom nested
in the size field equals "in":
∙ db.inventory.find( { "size.uom": "in" } )

∙ Specify Match using Query Operator

∙ A query filter document can use


the query operators to specify
conditions in the following form:
∙ copy

∙ copied

∙ { <field1>: { <operator1>: <value1> }, ... }

∙ The following query uses the less than


operator ($lt) on the field h embedded in
the size field:
∙ copy
∙ copied

∙ db.inventory.find( { "size.h": { $lt: 15 } } )

∙ Specify AND Condition

∙ The following query selects all


documents where the nested field h is
less than 15, the nested
field uom equals "in", and
the status field equals "D":
∙ copy

∙ copied

∙ db.inventory.find( { "size.h": { $lt: 15 }, "size.uom


": "in", status: "D" } )

∙ Query an Array

∙ This page provides examples of query


operations on array fields using
the db.collection.find() method in
the mongo shell. The examples on this
page use the inventory collection. To
populate the inventorycollection, run the
following:
∙ copy

∙ copied

∙ db.inventory.insertMany([
∙ { item: "journal", qty: 25, tags: ["blan
k", "red"], dim_cm: [ 14, 21 ] },
∙ { item: "notebook", qty: 50, tags: ["red", "bla
nk"], dim_cm: [ 14, 21 ] },
∙ { item: "paper", qty: 100, tags: ["red", "blank
", "plain"], dim_cm: [ 14, 21 ] },
∙ { item: "planner", qty: 75, tags: ["blank", "red"], di
m_cm: [ 22.85, 30 ] },
∙ { item: "postcard", qty: 45, tags: ["blue"], dim_c
m: [ 10, 15.25 ] }
∙ ]);
∙ You can run the operation in the web
shell below:

∙ Match an Array
∙ To specify equality condition on an
array, use the query
document { <field>: <value> } where <val
ue> is the exact array to match,
including the order of the elements.

∙ The following example queries for all


documents where the field tags value is
an array with exactly two
elements, "red" and "blank", in the
specified order:
∙ db.inventory.find( { tags: ["red", "blank"] } )

∙ If, instead, you wish to find an array that


contains both the
elements "red" and "blank", without
regard to order or other elements in the
array, use the $all operator:
∙ copy

∙ copied

∙ db.inventory.find( { tags: { $all: ["red", "blank"] } } )

∙ Query an Array for an


Element
∙ To query if the array field contains at
least one element with the specified
value, use the
filter { <field>:<value> } where <value> is
the element value.

∙ The following example queries for all


documents where tags is an array that
contains the string "red" as one of its
elements:
∙ copy

∙ copied

∙ db.inventory.find( { tags: "red" } )

∙ To specify conditions on the elements in


the array field, use query operators in
the query filter document:
∙ copy

∙ copied

∙ { <array field>: { <operator1>: <value1>, ... } }

∙ For example, the following operation


queries for all documents where the
array dim_cm contains at least one
element whose value is greater than 25.
∙ copy

∙ copied

∙ db.inventory.find( { dim_cm: { $gt: 25 } } )


∙ Specify Multiple
Conditions for Array
Elements
∙ When specifying compound conditions
on array elements, you can specify the
query such that either a single array
element meets these condition or any
combination of array elements meets
the conditions.
∙ Query an Array with Compound Filter
Conditions on the Array Elements

∙ The following example queries for


documents where the dim_cm array
contains elements that in some
combination satisfy the query
conditions; e.g., one element can satisfy
the greater than 15 condition and
another element can satisfy the less
than 20 condition, or a single element
can satisfy both:
∙ copy

∙ copied

∙ db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } )

∙ Query for an Array Element that Meets


Multiple Criteria

∙ Use $elemMatch operator to specify
multiple criteria on the elements of an
array such that at least one array
element satisfies all the specified
criteria.

∙ The following example queries for


documents where the dim_cm array
contains at least one element that is
both greater than ($gt) 22 and less than
($lt) 30:
∙ copy

∙ copied

∙ db.inventory.find( { dim_cm: { $elemMatch: { $gt:


22, $lt: 30 } } } )

∙ Query for an Element by the Array Index


Position

∙ Using dot notation, you can specify


query conditions for an element at a
particular index or position of the array.
The array uses zero-based indexing.
∙ NOTE
∙ When querying using dot notation, the
field and nested field must be inside
quotation marks.

∙ The following example queries for all


documents where the second element in
the array dim_cm is greater than 25:
∙ Copy

∙ copied

∙ db.inventory.find( { "dim_cm.1": { $gt: 25 } } )

∙ Query an Array by Array Length

∙ Use the $size operator to query for


arrays by number of elements. For
example, the following selects
documents where the array tags has 3
elements.
∙ copy

∙ copied

∙ db.inventory.find( { "tags": { $size: 3 } } )

∙ Query an Array of Embedded Documents


∙ This page provides examples of query
operations on an array of nested
documents using
thedb.collection.find() method in
the mongo shell. The examples on this
page use the inventorycollection. To
populate the inventory collection, run the
following:
∙ copy

∙ copied

∙ db.inventory.insertMany( [
∙ { item: "journal", instock: [ { warehouse: "A", qty:
5 }, { warehouse: "C", qty: 15 } ] },
∙ { item: "notebook", instock: [ { warehouse: "C",
qty: 5 } ] },
∙ { item: "paper", instock: [ { warehouse: "A", qty:
60 }, { warehouse: "B", qty: 15 } ] },
∙ { item: "planner", instock: [ { warehouse: "A", qt
y: 40 }, { warehouse: "B", qty: 5 } ] },
∙ { item: "postcard", instock: [ { warehouse: "B", q
ty: 15 }, { warehouse: "C", qty: 35 } ] }
∙ ]);

∙ You can run the operation in the web


shell below:


∙ Query for a Document


Nested in an Array
∙ The following example selects all
documents where an element in
the instock array matches the specified
document:
∙ copy

∙ copied

∙ db.inventory.find( { "instock": { warehouse: "A", qt


y: 5 } } )


∙ Equality matches on the whole


embedded/nested document require
an exact match of the specified
document, including the field order. For
example, the following query does not
match any documents in
the inventorycollection:
∙ copy

∙ copied

∙ db.inventory.find( { "instock": { qty: 5, warehouse:


"A" } } )

∙ Specify a Query Condition


on a Field in an Array of
Documents
∙ Specify a Query Condition on a Field
Embedded in an Array of Documents

∙ If you do not know the index position of


the document nested in the array,
concatenate the name of the array field,
with a dot (.) and the name of the field in
the nested document.

∙ The following example selects all


documents where the instock array has
at least one embedded document that
contains the field qty whose value is
less than or equal to 20:
∙ copy

∙ copied
∙ db.inventory.find( { 'instock.qty': { $lte: 20 } } )

∙ Use the Array Index to Query for a Field in


the Embedded Document

∙ Using dot notation, you can specify


query conditions for field in a document
at a particular index or position of the
array. The array uses zero-based
indexing.
∙ NOTE
∙ When querying using dot notation, the
field and index must be inside quotation
marks.

∙ The following example selects all


documents where the instock array has
as its first element a document that
contains the field qty whose value is
less than or equal to 20:
∙ copy
∙ copied

∙ db.inventory.find( { 'instock.0.qty': { $lte: 20 } } )

∙ Specify Multiple
Conditions for Array of
Documents
∙ When specifying conditions on more
than one field nested in an array of
documents, you can specify the query
such that either a single document
meets these condition or any
combination of documents (including a
single document) in the array meets the
conditions.
∙ A Single Nested Document Meets Multiple
Query Conditions on Nested Fields

∙ Use $elemMatch operator to specify
multiple criteria on an array of
embedded documents such that at least
one embedded document satisfies all
the specified criteria.

∙ The following example queries for


documents where the instock array has
at least one embedded document that
contains both the field qty equal
to 5 and the field warehouse equal to A:
∙ db.inventory.find( { "instock": { $elemMatch: { qt
y: 5, warehouse: "A" } } } )

∙ The following example queries for


documents where the instock array has
at least one embedded document that
contains the field qty that is greater
than 10 and less than or equal to 20:

∙ db.inventory.find( { "instock": { $elemMatch: { qt


y: { $gt: 10, $lte: 20 } } } } )

∙ Combination of Elements Satisfies the


Criteria

∙ If the compound query conditions on an


array field do not use
the $elemMatch operator, the query
selects those documents whose array
contains any combination of elements
that satisfies the conditions.

∙ For example, the following query


matches documents where any
document nested in the instock array
has the qty field greater than 10 and any
document (but not necessarily the same
embedded document) in the array has
the qty field less than or equal to 20:
∙ db.inventory.find( { "instock.qty": { $gt: 10, $lte:
20 } } )

∙ The following example queries for
documents where the instock array has
at least one embedded document that
contains the field qty equal to 5 and at
least one embedded document (but not
necessarily the same embedded
document) that contains the
field warehouse equal to A:
∙ db.inventory.find( { "instock.qty": 5, "instock.ware
house": "A" } )

Update Operations
Update operations modify
existing documents in a collection.
MongoDB provides the following methods
to update documents of a collection:
∙ db.collection.updateOne() 
∙ db.collection.updateMany() 
∙ db.collection.replaceOne() 

This page uses the following mongo shell


methods:

∙ db.collection.updateOne(<filter>, <update>, 
<options>)
∙ db.collection.updateMany(<filter>, <update>,
 <options>)
∙ db.collection.replaceOne(<filter>, <update>, 
<options>)
The examples on this page use
the inventory collection. To create and/or
populate the inventory collection, run the
following:

db.inventory.insertMany( [
{ item: "canvas", qty: 100, size: { h: 28, w: 35.5, uom:
"cm" }, status: "A" },
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "c
m" }, status: "A" },
{ item: "mat", qty: 85, size: { h: 27.9, w: 35.5, uom: "c
m" }, status: "A" },
{ item: "mousepad", qty: 25, size: { h: 19, w: 22.85, u
om: "cm" }, status: "P" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom:
"in" }, status: "P" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "i
n" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uo
m: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uo
m: "cm" }, status: "A" },
{ item: "sketchbook", qty: 80, size: { h: 14, w: 21, uo
m: "cm" }, status: "A" },
{ item: "sketch pad", qty: 95, size: { h: 22.85, w: 30.
5, uom: "cm" }, status: "A" }
] );

You can run the operation in the web shell


below:

Update Documents in a
Collection
To update a document, MongoDB
provides update operators, such as $set, to
modify field values.

To use the update operators, pass to the


update methods an update document of the
form:
{
<update operator>: { <field1>: <value1>, ... },
<update operator>: { <field2>: <value2>, ... },
...
}

Some update operators, such as $set, will


create the field if the field does not exist.
Update a Single Document

The following example uses


the db.collection.updateOne()
method on the inventory collection to
update the first document
where item equals "paper":

db.inventory.updateOne(
{ item: "paper" },
{
$set: { "size.uom": "cm", status: "P" },
$currentDate: { lastModified: true }
}
)

The update operation:



∙ uses the $set operator to update the value


of the size.uom field to "cm" and the value
of the statusfield to "P",
∙ uses the $currentDate operator to update
the value of the lastModified field to the
current date. IflastModified field does not
exist, $currentDate will create the field.
See $currentDate for details.

Update Multiple Documents

The following example uses


the db.collection.updateMany() method on
the inventory collection to update all
documents where qty is less than 50:
db.inventory.updateMany(
{ "qty": { $lt: 50 } },
{
$set: { "size.uom": "in", status: "P" },
$currentDate: { lastModified: true }
}
)

The update operation:


∙ uses the $set operator to update the value
of the size.uom field to "in" and the value of
the status field to "P",
∙ uses the $currentDate operator to update
the value of the lastModified field to the
current date. IflastModified field does not
exist, $currentDate will create the field.
See $currentDate for details.

Replace a Document
To replace the entire content of a document
except for the _id field, pass an entirely new
document as the second argument
to db.collection.replaceOne().

When replacing a document, the


replacement document must consist of
only field/value pairs; i.e. do not
include update operators expressions.

The replacement document can have


different fields from the original document.
In the replacement document, you can omit
the _id field since the _id field is immutable;
however, if you do include the _id field, it
must have the same value as the current
value.

The following example replaces


the first document from
the inventory collection where item: "paper":
copy
copied
db.inventory.replaceOne(
{ item: "paper" },
{ item: "paper", instock: [ { warehouse: "A", qty: 60 },
{ warehouse: "B", qty: 40 } ] }
)

Behavior
Atomicity

All write operations in MongoDB are atomic


on the level of a single document.
_id Field

Once set, you cannot update the value of


the _id field nor can you replace an existing
document with a replacement document
that has a different _id field value.
Document Size

When performing update operations that


increase the document size beyond the
allocated space for that document, the
update operation relocates the document
on disk.
Field Order

MongoDB preserves the order of the


document fields following write
operations except for the following cases:
∙ The _id field is always the first field in the
document.
∙ Updates that include renaming of field
names may result in the reordering of fields
in the document.

Changed in version 2.6: Starting in version


2.6, MongoDB actively attempts to preserve
the field order in a document. Before
version 2.6, MongoDB did not actively
preserve the order of the fields in a
document.
Upsert Option

If updateOne(), updateMany(),
or replaceOne() includes upsert : true and n
o documents match the specified filter,
then the operation creates a new document
and inserts it. If there are matching
documents, then the operation modifies or
replaces the matching document or
documents.
For details on the new document created,
see the individual reference pages for the
methods.

In MongoDB, update operations target a


single collection. All write operations in
MongoDB are atomic on the level of a
single document.

You can specify criteria, or filters, that


identify the documents to update.
These filters use the same syntax as read
operations.

For examples, see Update Documents.

Delete Operations
Delete operations remove documents from
a collection. MongoDB provides the
following methods to delete documents of
a collection:
∙ db.collection.deleteOne() New in version 3.2
∙ db.collection.deleteMany() New in version
3.2

In MongoDB, delete operations target a


single collection. All write operations in
MongoDB are atomic on the level of a
single document.

You can specify criteria, or filters, that


identify the documents to remove.
These filters use the same syntax as read
operations.

For examples, see Delete Documents.


CRUD
OPERATI
ONS
The use Command
MongoDB use DATABASE_NAME is used
to create database. The command will
create a new database if it doesn't exist,
otherwise it will return the existing
database.
Syntax
Basic syntax of use DATABASE statement
is as follows −
use DATABASE_NAME

Example
If you want to use a database with
name <mydb>, then use
DATABASEstatement would be as follows

>use mydb

switched to db mydb
To check your currently selected database,
use the command db
>db
mydb
If you want to check your databases list,
use the command show dbs.
>show dbs
local 0.78125GB
test 0.23012GB
Your created database (mydb) is not
present in list. To display database, you
need to insert at least one document into it.
>db.movie.insert({"name":"tutorials point"})

>show dbs

local 0.78125GB

mydb 0.23012GB

test 0.23012GB

In MongoDB default database is test. If you


didn't create any database, then collections
will be stored in test database.

The dropDatabase()
Method
MongoDB db.dropDatabase() command is
used to drop a existing 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

>

The createCollection()
Method
MongoDB db.createCollection(name,
options) is used to create collection.
Syntax
Basic syntax
of createCollection() command is as
follows −
db.createCollection(name, options)
In the command, name is name of
collection to be created. Options is a
document and is used to specify
configuration of collection.
Parameter Type Description
Name String Name of the
collection to be
created

Options Document (Optional) Specify


options about
memory size and
indexing

Options parameter is optional, so you need


to specify only the name of the collection.
Following is the list of options you can use

Field Type Description

(Optional) If true,
enables a capped
capped Boolean collection. Capped
collection is a fixed size
collection that
automatically
overwrites its oldest
entries when it reaches
its maximum size. If
you specify true, you
need to specify size
parameter also.

(Optional) If true,
automatically create
autoIndexId Boolean
index on _id field.s
Default value is false.

(Optional) Specifies a
maximum size in bytes
for a capped
size number
collection. If capped is
true, then you need to
specify this field also.

max number (Optional) Specifies the


maximum number of
documents allowed in
the capped collection.

While inserting the document, MongoDB


first checks size field of capped collection,
then it checks max field.
Examples
Basic syntax of createCollection() method
without options is as follows −
>use test

switched to db test

>db.createCollection("mycollection")

{ "ok" : 1 }

>

You can check the created collection by


using the command show collections.
>show collections
mycollection

system.indexes

The following example shows the syntax


of createCollection() method with few
important options −
>db.createCollection("mycol", { capped : true,
autoIndexId : true, size :

6142800, max : 10000 } )

{ "ok" : 1 }

>

In MongoDB, you don't need to create


collection. MongoDB creates collection
automatically, when you insert some
document.
>db.tutorialspoint.insert({"name" : "tutorialspoint"})

>show collections

mycol
mycollection

system.indexes

tutorialspoint

>

The drop() Method


MongoDB's db.collection.drop() is used to
drop a collection from the database.
Syntax
Basic syntax of drop() command is as
follows −
db.COLLECTION_NAME.drop()

Example
First, check the available collections into
your database mydb.
>use mydb
switched to db mydb

>show collections

mycol

mycollection

system.indexes

tutorialspoint

>

Now drop the collection with the


name mycollection.
>db.mycollection.drop()

true

>

Again check the list of collections into


database.
>show collections

mycol
system.indexes

tutorialspoint

>

drop() method will return true, if the


selected collection is dropped successfully,
otherwise it will return false.
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.
∙ In this chapter, we will learn how to
insert document in MongoDB collection.

∙ The insert() Method


∙ To insert data into MongoDB collection,
you need to use
MongoDB's insert()or save() method.
∙ Syntax
∙ The basic syntax of insert() command is
as follows −
∙ >db.COLLECTION_NAME.insert(document)

∙ Example
∙ >db.mycol.insert({

∙ _id: ObjectId(7df78ad8902c),

∙ title: 'MongoDB Overview',

∙ description: 'MongoDB is no sql database',


∙ by: 'tutorials point',

∙ url: 'http://www.tutorialspoint.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: 'tutorials point',

∙ url: 'http://www.tutorialspoint.com',

∙ tags: ['mongodb', 'database', 'NoSQL'],

∙ likes: 100

∙ },


∙ {

∙ title: 'NoSQL Database',

∙ description: "NoSQL database doesn't have


tables",

∙ by: 'tutorials point',

∙ url: 'http://www.tutorialspoint.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.

The find() Method


To query data from MongoDB collection,
you need to use MongoDB's find()method.
Syntax
The basic syntax of find() method is as
follows −
>db.COLLECTION_NAME.find()
find() method will display all the
documents in a non-structured way.

The pretty() Method


To display the results in a formatted way,
you can use pretty() method.
Syntax
>db.mycol.find().pretty()

Example
>db.mycol.find().pretty()

"_id": ObjectId(7df78ad8902c),

"title": "MongoDB Overview",

"description": "MongoDB is no sql database",

"by": "tutorials point",


"url": "http://www.tutorialspoint.com",

"tags": ["mongodb", "database", "NoSQL"],

"likes": "100"

>

Apart from find() method, there


is findOne() method, that returns only one
document.

RDBMS Where Clause


Equivalents in
MongoDB
To query the document on the basis of
some condition, you can use following
operations.
Oper Syntax Example RDB
ation MS
Equiv
alent

Equal {<key>:<valu db.mycol.find({"by":"tut where


ity e>} orials point"}).pretty() by =
'tutori
als
point'

Less {<key>:{$lt:<v db.mycol.find({"likes": where


Than alue>}} {$lt:50}}).pretty() likes
< 50

Less {<key>:{$lte:< db.mycol.find({"likes": where


Than value>}} {$lte:50}}).pretty() likes
Equal <= 50
s

Great {<key>:{$gt:<v db.mycol.find({"likes": where


er alue>}} {$gt:50}}).pretty() likes
Than > 50

Great {<key>:{$gte: db.mycol.find({"likes": where


er <value>}} {$gte:50}}).pretty() likes
Than >= 50
Equal
s

Not {<key>:{$ne:< db.mycol.find({"likes": where


Equal value>}} {$ne:50}}).pretty() likes
s != 50

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 'tutorials point' and
whose title is 'MongoDB Overview'.
>db.mycol.find({$and:[{"by":"tutorials point"},{"title":
"MongoDB Overview"}]}).pretty() {

"_id": ObjectId(7df78ad8902c),

"title": "MongoDB Overview",

"description": "MongoDB is no sql database",

"by": "tutorials point",

"url": "http://www.tutorialspoint.com",

"tags": ["mongodb", "database", "NoSQL"],

"likes": "100"
}

For the above given example, equivalent


where clause will be ' where by = 'tutorials
point' 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 $orkeyword.
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 'tutorials point' or
whose title is 'MongoDB Overview'.
>db.mycol.find({$or:[{"by":"tutorials point"},{"title":
"MongoDB Overview"}]}).pretty()

"_id": ObjectId(7df78ad8902c),

"title": "MongoDB Overview",

"description": "MongoDB is no sql database",

"by": "tutorials point",

"url": "http://www.tutorialspoint.com",

"tags": ["mongodb", "database", "NoSQL"],

"likes": "100"
}

>

Using AND and OR


Together
Example
The following example will show the
documents that have likes greater than 10
and whose title is either 'MongoDB
Overview' or by is 'tutorials point'.
Equivalent SQL where clause is 'where
likes>10 AND (by = 'tutorials point' OR title
= 'MongoDB Overview')'
>db.mycol.find({"likes": {$gt:10}, $or: [{"by": "tutorials
point"},

{"title": "MongoDB Overview"}]}).pretty()

{
"_id": ObjectId(7df78ad8902c),

"title": "MongoDB Overview",

"description": "MongoDB is no sql database",

"by": "tutorials point",

"url": "http://www.tutorialspoint.com",

"tags": ["mongodb", "database", "NoSQL"],

"likes": "100"

>

MongoDB's update() and save() methods
are used to update document into a
collection. The update() method updates
the values in the existing document while
the save() method replaces the existing
document with the document passed in
save() method.
MongoDB Update()
Method
The update() method updates the values in
the existing document.
Syntax
The basic syntax of update() method is as
follows −
>db.COLLECTION_NAME.update(SELECTION_CRITERIA,
UPDATED_DATA)

Example
Consider the mycol collection has the
following data.
{ "_id" : ObjectId(5983548781331adf45ec5),
"title":"MongoDB Overview"}

{ "_id" : ObjectId(5983548781331adf45ec6),
"title":"NoSQL Overview"}

{ "_id" : ObjectId(5983548781331adf45ec7),
"title":"Tutorials Point Overview"}

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":"Tutorials Point 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
The basic syntax of
MongoDB save() method is shown below −
>db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DAT
A})

Example
Following example will replace the
document with the _id
'5983548781331adf45ec5'.
>db.mycol.save(

"_id" : ObjectId(5983548781331adf45ec7),
"title":"Tutorials Point New Topic",

"by":"Tutorials Point"

>db.mycol.find()

{ "_id" : ObjectId(5983548781331adf45ec5),
"title":"Tutorials Point New Topic",

"by":"Tutorials Point"}

{ "_id" : ObjectId(5983548781331adf45ec6),
"title":"NoSQL Overview"}

{ "_id" : ObjectId(5983548781331adf45ec7),
"title":"Tutorials Point Overview"}

>
The remove() Method
MongoDB's remove() method is used to
remove a document from the collection.
remove() method accepts two parameters.
One is deletion criteria and second is
justOne flag.
∙ deletion criteria − (Optional) deletion criteria
according to documents will be removed.
∙ justOne − (Optional) if set to true or 1, then
remove only one document.
Syntax
Basic syntax of remove() method is as
follows −
>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)

Example
Consider the mycol collection has the
following data.
{ "_id" : ObjectId(5983548781331adf45ec5),
"title":"MongoDB Overview"}

{ "_id" : ObjectId(5983548781331adf45ec6),
"title":"NoSQL Overview"}

{ "_id" : ObjectId(5983548781331adf45ec7),
"title":"Tutorials Point Overview"}

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":"Tutorials Point Overview"}

>

Remove Only One


If there are multiple records and you want
to delete only the first record, then
set justOne parameter in remove() method.
>db.COLLECTION_NAME.remove(DELETION_CRITER
IA,1)

Remove All
Documents
If you don't specify deletion criteria, then
MongoDB will delete whole documents
from the collection. This is equivalent of
SQL's truncate command.
>db.mycol.remove()

>db.mycol.find()

>

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.

The find() Method


MongoDB's find() method, explained
in MongoDB Query Document accepts
second optional parameter that is list of
fields that you want to retrieve. In
MongoDB, when you
execute find() method, then it displays all
fields of a document. To limit this, you
need to set a list of fields with value 1 or 0.
1 is used to show the field while 0 is used
to hide the fields.
Syntax
The basic syntax of find() method with
projection is as follows −
>db.COLLECTION_NAME.find({},{KEY:1})
Example
Consider the collection mycol has the
following data −
{ "_id" : ObjectId(5983548781331adf45ec5),
"title":"MongoDB Overview"}

{ "_id" : ObjectId(5983548781331adf45ec6),
"title":"NoSQL Overview"}

{ "_id" : ObjectId(5983548781331adf45ec7),
"title":"Tutorials Point Overview"}

Following example will display the title of


the document while querying the
document.
>db.mycol.find({},{"title":1,_id:0})

{"title":"MongoDB Overview"}

{"title":"NoSQL Overview"}

{"title":"Tutorials Point Overview"}

>
Please note _id field is always displayed
while executing find() method, if you don't
want this field, then you need to set it as 0.

The Limit() Method


To limit the records in MongoDB, you need
to use limit() method. The method accepts
one number type argument, which is the
number of documents that you want to be
displayed.
Syntax
The basic syntax of limit() method is as
follows −
>db.COLLECTION_NAME.find().limit(NUMBER)

Example
Consider the collection myycol has the
following data.
{ "_id" : ObjectId(5983548781331adf45ec5),
"title":"MongoDB Overview"}
{ "_id" : ObjectId(5983548781331adf45ec6),
"title":"NoSQL Overview"}

{ "_id" : ObjectId(5983548781331adf45ec7),
"title":"Tutorials Point Overview"}

Following example will display only two


documents while querying the document.
>db.mycol.find({},{"title":1,_id:0}).limit(2)

{"title":"MongoDB Overview"}

{"title":"NoSQL Overview"}

>

If you don't specify the number argument


in limit() method then it will display all
documents from the collection.

MongoDB Skip()
Method
Apart from limit() method, there is one
more method skip() which also accepts
number type argument and is used to skip
the number of documents.
Syntax
The basic syntax of skip() method is as
follows −
>db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMB
ER)

Example
Following example will display only the
second document.
>db.mycol.find({},{"title":1,_id:0}).limit(1).skip(1)

{"title":"NoSQL Overview"}

>

Please note, the default value


in skip() method is 0.

The sort() Method


To sort documents in MongoDB, you need
to use sort() method. The method accepts
a document containing a list of fields along
with their sorting order. To specify sorting
order 1 and -1 are used. 1 is used for
ascending order while -1 is used for
descending order.
Syntax
The basic syntax of sort() method is as
follows −
>db.COLLECTION_NAME.find().sort({KEY:1})

Example
Consider the collection myycol has the
following data.
{ "_id" : ObjectId(5983548781331adf45ec5),
"title":"MongoDB Overview"}

{ "_id" : ObjectId(5983548781331adf45ec6),
"title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7),
"title":"Tutorials Point Overview"}

Following example will display the


documents sorted by title in the
descending order.
>db.mycol.find({},{"title":1,_id:0}).sort({"title":-1})

{"title":"Tutorials Point Overview"}

{"title":"NoSQL Overview"}

{"title":"MongoDB Overview"}

>

Please note, if you don't specify the sorting


preference, then sort() method will display
the documents in ascending order.
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.

The ensureIndex()
Method
To create an index you need to use
ensureIndex() method of MongoDB.
Syntax
The basic syntax of ensureIndex() method
is as follows().
>db.COLLECTION_NAME.ensureIndex({KEY:1})

Here key is the name of the field on which


you want to create index and 1 is for
ascending order. To create index in
descending order you need to use -1.
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 −
Parameter Type Description

background Boolean Builds the index


in the
background so
that building an
index does not
block other
database
activities.
Specify true to
build in the
background.
The default
value is false.

Creates a
unique index so
that the
unique Boolean collection will
not accept
insertion of
documents
where the index
key or keys
match an
existing value
in the index.
Specify true to
create a unique
index. The
default value
is false.

The name of
the index. If
unspecified,
MongoDB
generates an
name string index name by
concatenating
the names of
the indexed
fields and the
sort order.
Creates a
unique index on
a field that may
have
duplicates.
MongoDB
indexes only
the first
occurrence of a
key and
dropDups Boolean removes all
documents
from the
collection that
contain
subsequent
occurrences of
that key.
Specify true to
create unique
index. The
default value
is false.

If true, the
index only
references
documents
with the
specified field.
These indexes
use less space
sparse Boolean
but behave
differently in
some
situations
(particularly
sorts). The
default value
is false.

expireAfterSeconds integer Specifies a


value, in
seconds, as a
TTL to control
how long
MongoDB
retains
documents in
this collection.

The index
version
number. The
default index
version
index
v depends on the
version
version of
MongoDB
running when
creating the
index.
The weight is a
number ranging
from 1 to
99,999 and
denotes the
weights document significance of
the field
relative to the
other indexed
fields in terms
of the score.

For a text index,


the language
that determines
the list of stop
default_language string words and the
rules for the
stemmer and
tokenizer. The
default value
is english.

For a text index,


specify the
name of the
field in the
document that
contains, the
language_override string
language to
override the
default
language. The
default value is
language.

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.

The aggregate()
Method
For the aggregation in MongoDB, you
should use aggregate() method.
Syntax
Basic syntax of aggregate() method is as
follows −
>db.COLLECTION_NAME.aggregate(AGGREGATE_OPERA
TION)

Example
In the collection you have the following
data −
{

_id: ObjectId(7df78ad8902c)
title: 'MongoDB Overview',

description: 'MongoDB is no sql database',

by_user: 'tutorials point',

url: 'http://www.tutorialspoint.com',

tags: ['mongodb', 'database', 'NoSQL'],

likes: 100

},

_id: ObjectId(7df78ad8902d)

title: 'NoSQL Overview',

description: 'No sql database is very fast',

by_user: 'tutorials point',

url: 'http://www.tutorialspoint.com',

tags: ['mongodb', 'database', 'NoSQL'],

likes: 10
},

_id: ObjectId(7df78ad8902e)

title: 'Neo4j Overview',

description: 'Neo4j is no sql database',

by_user: 'Neo4j',

url: 'http://www.neo4j.com',

tags: ['neo4j', 'database', 'NoSQL'],

likes: 750

},

Now from the above collection, if you want


to display a list stating how many tutorials
are written by each user, then you will use
the following aggregate() method −
> db.mycol.aggregate([{$group : {_id : "$by_user",
num_tutorial : {$sum : 1}}}])
{

"result" : [

"_id" : "tutorials point",

"num_tutorial" : 2

},

"_id" : "Neo4j",

"num_tutorial" : 1

],

"ok" : 1

>

Sql equivalent query for the above use case


will be select by_user, count(*) from mycol
group by by_user.
In the above example, we have grouped
documents by field by_user and on each
occurrence of by_user previous value of
sum is incremented. Following is a list of
available aggregation expressions.
Expressio Description Example
n

Sums up the db.mycol.aggregate([{$gro


defined up : {_id : "$by_user",
value from num_tutorial : {$sum :
$sum all "$likes"}}}])
documents
in the
collection.

$avg Calculates db.mycol.aggregate([{$gro


the average up : {_id : "$by_user",
of all given num_tutorial : {$avg :
values from "$likes"}}}])
all
documents
in the
collection.

Gets the db.mycol.aggregate([{$gro


minimum of up : {_id : "$by_user",
the num_tutorial : {$min :
correspondi "$likes"}}}])
$min ng values
from all
documents
in the
collection.

Gets the db.mycol.aggregate([{$gro


maximum up : {_id : "$by_user",
$max
of the num_tutorial : {$max :
correspondi "$likes"}}}])
ng values
from all
documents
in the
collection.

Inserts the db.mycol.aggregate([{$gro


value to an up : {_id : "$by_user", url :
$push array in the {$push: "$url"}}}])
resulting
document.

Inserts the db.mycol.aggregate([{$gro


value to an up : {_id : "$by_user", url :
array in the {$addToSet : "$url"}}}])
$addToSe resulting
t document
but does
not create
duplicates.
Gets the db.mycol.aggregate([{$gro
first up : {_id : "$by_user",
document first_url : {$first : "$url"}}}])
from the
source
documents
according to
the
grouping.
$first
Typically
this makes
only sense
together
with some
previously
applied
“$sort”
-stage.

$last Gets the db.mycol.aggregate([{$gro


last up : {_id : "$by_user",
document last_url : {$last : "$url"}}}])
from the
source
documents
according to
the
grouping.
Typically
this makes
only sense
together
with some
previously
applied
“$sort”
-stage.

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.
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

How Replication
Works in MongoDB
MongoDB achieves replication by the use
of replica set. A replica set is a group
of mongod instances that host the same
data set. In a replica, one node is primary
node that receives all write operations. All
other instances, such as secondaries, apply
operations from the primary so that they
have the same data set. Replica set can
have only one primary node.
∙ Replica set is a group of two or more nodes
(generally minimum 3 nodes are required).
∙ In a replica set, one node is primary node and
remaining nodes are secondary.
∙ All data replicates from primary to secondary
node.
∙ At the time of automatic failover or
maintenance, election establishes for primary
and a new primary node is elected.
∙ After the recovery of failed node, it again join the
replica set and works as a secondary node.
A typical diagram of MongoDB replication
is shown in which client application always
interact with the primary node and the
primary node then replicates the data to
the secondary nodes.
Replica Set Features
∙ A cluster of N nodes
∙ Any one node can be primary
∙ All write operations go to primary
∙ Automatic failover
∙ Automatic recovery
∙ Consensus election of primary

Set Up a Replica Set


In this tutorial, we will convert standalone
MongoDB instance to a replica set. To
convert to replica set, following are the
steps −
∙ Shutdown already running MongoDB server.

∙ Start the MongoDB server by specifying --


replSet option. Following is the basic syntax of
--replSet −
mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH"
--replSet "REPLICA_SET_INSTANCE_NAME"

Example
mongod --port 27017 --dbpath "D:\set
up\mongodb\data" --replSet rs0
∙ It will start a mongod instance with the name
rs0, on port 27017.
∙ Now start the command prompt and connect to
this mongod instance.
∙ In Mongo client, issue the
command rs.initiate() to initiate a new replica
set.
∙ To check the replica set configuration, issue the
command rs.conf(). To check the status of
replica set issue the command rs.status().

Add Members to
Replica Set
To add members to replica set, start
mongod instances on multiple machines.
Now start a mongo client and issue a
command rs.add().
Syntax
The basic syntax of rs.add() command is
as follows −
>rs.add(HOST_NAME:PORT)

Example
Suppose your mongod instance name
is mongod1.net and it is running on
port 27017. To add this instance to replica
set, issue the command rs.add()in Mongo
client.
>rs.add("mongod1.net:27017")

>

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.
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.

In the following diagram, there are three


main components −
∙ Shards − Shards are used to store data. They
provide high availability and data consistency. In
production environment, each shard is a
separate replica set.
∙ Config Servers − Config servers store the
cluster's metadata. This data contains a
mapping of the cluster's data set to the shards.
The query router uses this metadata to target
operations to specific shards. In production
environment, sharded clusters have exactly 3
config servers.
∙ Query Routers − Query routers are basically
mongo instances, interface with client
applications and direct operations to the
appropriate shard. The query router processes
and targets the operations to shards and then
returns results to the clients. A sharded cluster
can contain more than one query router to
divide the client request load. A client sends
requests to one query router. Generally, a
sharded cluster have many query routers.

Dump MongoDB Data


To create backup of database in MongoDB,
you should use mongodumpcommand.
This command will dump the entire data of
your server into the dump directory. There
are many options available by which you
can limit the amount of data or create
backup of your remote server.
Syntax
The basic syntax
of mongodump command is as follows −
>mongodump

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 −

Following is a list of available options that


can be used with
the mongodumpcommand.
Syntax Description Example

mongodump --host This mongodump


HOST_NAME --port commmand --host
PORT_NUMBER will backup tutorialspoint.co
all m --port 27017
databases
of specified
mongod
instance.

mongodump This mongodump


--dbpath DB_PATH command --dbpath
--out will backup /data/db/ --out
BACKUP_DIRECTOR only /data/backup/
Y specified
database at
specified
path.

mongodump This mongodump


--collection command --collection
COLLECTION --db will backup mycol --db test
DB_NAME only
specified
collection
of specified
database.

Restore data
To restore backup data
MongoDB's mongorestore command is
used. This command restores all of the
data from the backup directory.
Syntax
The basic syntax
of mongorestore command is −
>mongorestore
Following is the output of the command −
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 −
To change mongotop command to return
information less frequently, specify a
specific number after the mongotop
command.
D:\set up\mongodb\bin>mongotop 30
The above example will return values every
30 seconds.
Apart from the MongoDB tools, 10gen
provides a free, hosted monitoring service,
MongoDB Management Service (MMS),
that provides a dashboard and gives you a
view of the metrics from your entire
cluster.
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;

public class ConnectToDB {

public static void main( String args[] ) {

// Creating a Mongo client

MongoClient mongo = new MongoClient(


"localhost" , 27017 );

// Creating Credentials

MongoCredential credential;

credential =
MongoCredential.createCredential("sampleUser",
"myDb",

"password".toCharArray());
System.out.println("Connected to the database
successfully");

// Accessing the database

MongoDatabase database =
mongo.getDatabase("myDb");

System.out.println("Credentials ::"+ credential);

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 cl
ass is used.
Following is the code snippet to create a
collection −
import com.mongodb.client.MongoDatabase;

import com.mongodb.MongoClient;

import com.mongodb.MongoCredential;

public class CreatingCollection {


public static void main( String args[] ) {

// Creating a Mongo client

MongoClient mongo = new MongoClient(


"localhost" , 27017 );

// Creating Credentials

MongoCredential credential;

credential =
MongoCredential.createCredential("sampleUser",
"myDb",

"password".toCharArray());

System.out.println("Connected to the database


successfully");

//Accessing the database

MongoDatabase database =
mongo.getDatabase("myDb");
//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 cl
ass 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;

public class selectingCollection {

public static void main( String args[] ) {

// Creating a Mongo client

MongoClient mongo = new MongoClient(


"localhost" , 27017 );
// Creating Credentials

MongoCredential credential;

credential =
MongoCredential.createCredential("sampleUser",
"myDb",

"password".toCharArray());

System.out.println("Connected to the database


successfully");

// Accessing the database

MongoDatabase database =
mongo.getDatabase("myDb");

// 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");

On compiling, the above program gives you


the following result −
Connected to the database successfully
Collection created successfully
Collection myCollection selected successfully

Insert a Document
To insert a document into
MongoDB, insert() method
of com.mongodb.client.MongoCollection cl
ass 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;

public class InsertingDocument {

public static void main( String args[] ) {

// Creating a Mongo client

MongoClient mongo = new MongoClient(


"localhost" , 27017 );

// Creating Credentials
MongoCredential credential;

credential =
MongoCredential.createCredential("sampleUser",
"myDb",

"password".toCharArray());

System.out.println("Connected to the database


successfully");

// Accessing the database

MongoDatabase database =
mongo.getDatabase("myDb");

// Retrieving a collection

MongoCollection<Document> collection =
database.getCollection("sampleCollection");

System.out.println("Collection sampleCollection
selected successfully");
Document document = new Document("title",
"MongoDB")

.append("id", 1)

.append("description", "database")

.append("likes", 100)

.append("url",
"http://www.tutorialspoint.com/mongodb/")

.append("by", "tutorials point");

collection.insertOne(document);

System.out.println("Document inserted
successfully");

On compiling, the above program gives you


the following result −
Connected to the database successfully
Collection sampleCollection selected successfully
Document inserted successfully

Retrieve All
Documents
To select all documents from the
collection, find() method
of com.mongodb.client.MongoCollection cl
ass is used. This method returns a cursor,
so you need to iterate this cursor.
Following is the program to select all
documents −
import com.mongodb.client.FindIterable;

import com.mongodb.client.MongoCollection;

import com.mongodb.client.MongoDatabase;

import java.util.Iterator;

import org.bson.Document;
import com.mongodb.MongoClient;

import com.mongodb.MongoCredential;

public class RetrievingAllDocuments {

public static void main( String args[] ) {

// Creating a Mongo client

MongoClient mongo = new MongoClient(


"localhost" , 27017 );

// Creating Credentials

MongoCredential credential;

credential =
MongoCredential.createCredential("sampleUser",
"myDb",

"password".toCharArray());
System.out.println("Connected to the database
successfully");

// Accessing the database

MongoDatabase database =
mongo.getDatabase("myDb");

// Retrieving a collection

MongoCollection<Document> collection =
database.getCollection("sampleCollection");

System.out.println("Collection sampleCollection
selected successfully");

// Getting the iterable object

FindIterable<Document> iterDoc =
collection.find();

int i = 1;
// Getting the iterator

Iterator it = iterDoc.iterator();

while (it.hasNext()) {

System.out.println(it.next());

i++;

On compiling, the above program gives you


the following result −
Document{{
_id = 5967745223993a32646baab8,
title = MongoDB,
id = 1,
description = database,
likes = 100,
url = http://www.tutorialspoint.com/mongodb/, by =
tutorials point
}}
Document{{
_id = 7452239959673a32646baab8,
title = RethinkDB,
id = 2,
description = database,
likes = 200,
url = http://www.tutorialspoint.com/rethinkdb/, by =
tutorials point
}}

Update Document
To update a document from the
collection, updateOne() method
of com.mongodb.client.MongoCollection cl
ass 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;

public class UpdatingDocuments {

public static void main( String args[] ) {

// Creating a Mongo client

MongoClient mongo = new MongoClient(


"localhost" , 27017 );
// Creating Credentials

MongoCredential credential;

credential =
MongoCredential.createCredential("sampleUser",
"myDb",

"password".toCharArray());

System.out.println("Connected to the database


successfully");

// Accessing the database

MongoDatabase database =
mongo.getDatabase("myDb");

// 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...");

// Retrieving the documents after updation

// Getting the iterable object

FindIterable<Document> iterDoc =
collection.find();

int i = 1;

// Getting the iterator

Iterator it = iterDoc.iterator();

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.tutorialspoint.com/mongodb/, by =
tutorials point
}}

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;

public class DeletingDocuments {

public static void main( String args[] ) {


// Creating a Mongo client

MongoClient mongo = new MongoClient(


"localhost" , 27017 );

// Creating Credentials

MongoCredential credential;

credential =
MongoCredential.createCredential("sampleUser",
"myDb",

"password".toCharArray());

System.out.println("Connected to the database


successfully");

// Accessing the database

MongoDatabase database =
mongo.getDatabase("myDb");
// Retrieving a collection

MongoCollection<Document> collection =
database.getCollection("sampleCollection");

System.out.println("Collection sampleCollection
selected successfully");

// Deleting the documents

collection.deleteOne(Filters.eq("id", 1));

System.out.println("Document deleted
successfully...");

// Retrieving the documents after updation

// Getting the iterable object

FindIterable<Document> iterDoc =
collection.find();

int i = 1;
// Getting the iterator

Iterator it = iterDoc.iterator();

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 {

public static void main( String args[] ) {


// Creating a Mongo client

MongoClient mongo = new MongoClient(


"localhost" , 27017 );

// Creating Credentials

MongoCredential credential;

credential =
MongoCredential.createCredential("sampleUser",
"myDb",

"password".toCharArray());

System.out.println("Connected to the database


successfully");

// Accessing the database

MongoDatabase database =
mongo.getDatabase("myDb");
// 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");

On compiling, the above program gives you


the following result −
Connected to the database successfully
Collection sampleCollection selected successfully
Collection dropped successfully

Listing All the


Collections
To list all the collections in a database, you
need to use
the listCollectionNames() method of
the com.mongodb.client.MongoDatabase c
lass.
Following is the program to list all the
collections of a database −
import com.mongodb.client.MongoDatabase;

import com.mongodb.MongoClient;

import com.mongodb.MongoCredential;

public class ListOfCollection {


public static void main( String args[] ) {

// Creating a Mongo client

MongoClient mongo = new MongoClient(


"localhost" , 27017 );

// Creating Credentials

MongoCredential credential;

credential =
MongoCredential.createCredential("sampleUser",
"myDb",

"password".toCharArray());

System.out.println("Connected to the database


successfully");
// Accessing the database

MongoDatabase database =
mongo.getDatabase("myDb");

System.out.println("Collection created
successfully");

for (String name :


database.listCollectionNames()) {

System.out.println(name);

On compiling, the above program gives you


the following result −
Connected to the database successfully
Collection created successfully
myCollection
myCollection1
myCollection5
Remaining MongoDB methods save(),
limit(), skip(), sort() etc. work same as
explained in the subsequent tutorial.

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

Make a Connection
and Select a Database
To make a connection, 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 −
<?php

// connect to mongodb

$m = new MongoClient();

echo "Connection to database successfully";

// select a database

$db = $m->mydb;

echo "Database mydb selected";

?>

When the program is executed, it will


produce the following result −
Connection to database successfully
Database mydb selected

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";

?>

When the program is executed, it will


produce the following result −
Connection to database successfully
Database mydb selected
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.tutorialspoint.com/mongodb/",

"by" => "tutorials point"

);
$collection->insert($document);

echo "Document inserted successfully";

?>

When the program is executed, it will


produce the following result −
Connection to database successfully
Database mydb selected
Collection selected succsessfully
Document inserted successfully

Find All Documents


To select all documents from the
collection, find() method is used.
Following is the code snippet to select all
documents −
<?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";

$cursor = $collection->find();

// iterate cursor to display title of documents

foreach ($cursor as $document) {

echo $document["title"] . "\n";

}
?>

When the program is executed, it will


produce the following result −
Connection to database successfully
Database mydb selected
Collection selected succsessfully {
"title": "MongoDB"
}

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";

// now update the document

$collection->update(array("title"=>"MongoDB"),

array('$set'=>array("title"=>"MongoDB Tutorial")));

echo "Document updated successfully";

// now display the updated document

$cursor = $collection->find();
// iterate cursor to display title of documents

echo "Updated document";

foreach ($cursor as $document) {

echo $document["title"] . "\n";

?>

When the program is executed, it will


produce the following result −
Connection to database successfully
Database mydb selected
Collection selected succsessfully
Document updated successfully
Updated document {
"title": "MongoDB Tutorial"
}

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";


// now remove the document

$collection->remove(array("title"=>"MongoDB
Tutorial"),false);

echo "Documents deleted successfully";

// now display the available documents

$cursor = $collection->find();

// iterate cursor to display title of documents

echo "Updated document";

foreach ($cursor as $document) {

echo $document["title"] . "\n";

?>
When the program is executed, it will
produce the following result −
Connection to database successfully
Database mydb selected
Collection selected succsessfully
Documents deleted successfully
In the above example, the second
parameter is boolean type and used
for justOne field of remove() method.
Remaining MongoDB methods findOne(),
save(), limit(), skip(), sort()etc. works same
as explained above.
Comparison Query operators
Nam Description
e

$gt Matches values that are greater than the value


specified in the query.

$lt Matches values that are less than the value


specified in the query.
$gte Matches values that are greater than or equal
to the value specified in the query.

$lte Matches values that are less than or equal to


the value specified in the query.

$ne Matches all values that are not equal to the


value specified in the query.

$in Matches any of the values that exist in an array


specified in the query.

$nin Matches values that do not exist in an array


specified in the query.

Logical Query operators


Nam Description
e

$and Joins query clauses with a logical AND returns


all documents that match the conditions of
both clauses.

$not Inverts the effect of a query expression and


returns documents that do not match the query
expression.

$or Joins query clauses with a logical OR returns all


documents that match the conditions of either
clause.

$nor Joins query clauses with a logical NOR returns


all documents that fail to match both clauses.

Element Query operators


Name Description
$exists Matches documents that have the specified field.

$type Selects documents if a field is of the specified type.

Evaluation Query operators


Name Description

$mod Performs a modulo operation on the value of a field


and selects documents with a specified result.

$regex Selects documents where values match a specified


regular expression.

$where Matches documents that satisfy a JavaScript


expression.

Query operator Array


Name Description

$all Matches arrays that contain all


elements specified in the query.

$elemMatc Selects documents if an element in the


h array field matches all the specified
$elemMatch conditions.

$size Selects documents if the array field is a


specified size.

Projection operator
Name Description

$ Projects the first element


in an array that matches
the query condition.

$elemMatch(projectio Projects the first element


n) in an array that matches
the specified $elemMatch
condition.
$slice Limits the number of
elements projected from
an array. Supports skip and
limit slices.

Вам также может понравиться