2. A Brief History…
MongoDB (derived from "humongous") is a cross-platform documentoriented database system. Classified as a NoSQL database, MongoDB avoids
the traditional table-based relational database structure in favor of JSON-like
documents with dynamic schemas (Binary-encoded JSON representation),
making the integration of data in certain types of applications easier and
faster.
First developed by 10gen (now MongoDB Inc.) in October 2007 as a
component of a planned platform as a service product, the company shifted to
an open source development model in 2009, with 10gen offering commercial
support and other services. Since then, MongoDB has been adopted as
backend software by a number of major websites and services, including
Craigslist, eBay, Foursquare, SourceForge, and The New York Times, among
others. MongoDB is the most popular NoSQL database system.
Released under a combination of the GNU Affero General Public License and the Apache License, MongoDB is free and open source
software.
3. NoSQL
NoSQL is intended as shorthand for "not only SQL."
Complete architectures almost always mix traditional and NoSQL databases.
Brewer's (CAP) Theorem
It's impossible for a distributed computer system to
simultaneously provide all three of these
guarantees:
• Consistency (all nodes see the same data at the
same time)
• Availability (node failures don't prevent
survivors from continuing to operate)
• Partition tolerance (no failures less than total
network failures cause the system to fail)
*NoSQL's primary goal is to achieve horizontal scalability.
It attains this by reducing transactional semantics and
referential integrity.
4. JSON
JavaScript Object Notation
JSON is a lightweight data-interchange format. It is easy for humans to read and write.
It is easy for machines to parse and generate. It is based on a subset of the JavaScript
Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a
text format that is completely language independent but uses conventions that are
familiar to programmers of the C-family of languages, including C, C++, C#, Java,
JavaScript, Perl Python, and many others. These properties make JSON an ideal datainterchange language.
JSON is built on two structures:
•
A collection of name/value pairs. In
various languages, this is realized
as an
object, record, struct, dictionary, ha
sh table, keyed list, or associative
array.
•
An ordered list of values. In most
languages, this is realized as an
array, vector, list, or sequence.
*Complex Objects in JSON
9. Naming Rules
Databases
Database names cannot contain “.”, “$”, or “0” (the null character). Names can only contain
characters that can be used on your file system as filenames. Admin, config, and local are
reserved database names (you can store your own data in them, but you should never drop
them).
Collections
Collection names cannot contain “$” or “0”. Names prefixed with “system.” are reserved by
MongoDB and cannot be dropped (even if you created the collection). Dots are often used for
organization in collection names, but they have no semantic importance. A collection named
“che.se” has no more relation to a collection named “che” than one named “cheese” does.
Field Names
Field names cannot contain “.” nor “0”. Fields should only contain “$” when they are
database references.
10. Insert Documents
•
Save / Insert Function
Both functions does the same functionality
db.collection.save(<document>)
db.collection.insert(<document>)
Command
db.books.save( { item: "Divine Comedy”, price: 18, stock: 5 } )
db.books.insert({
”_id": 1,
"name": "Jaya Naresh K",
"department": [
{
"id": "D001",
"name": “Tech."
}
]
})
•
The _id Field
The _id field has the following behavior and constraints:
1.
2.
In documents, the _id field is always indexed for regular collections.
The _id field may contain values of any BSON data type, other than an array.
11. Query Operators
•
Query Format
Queries are generally of the form (JSON):
{key : value} or {key : {$op : value}}
For example
{age : 18} or {age : {$gte : 18}}
db.collection.find({age : 18}) or db.collection.findOne({age : 18})
There are three exceptions to this rule: $and, $or, and $nor, which are all top-level:
{$or : [{age: {$gte : 18}}, {age : {$lt : 18}]}
{$and : [{age: 18}}, {age : 19}]},
•
Dot Notation
MongoDB uses the dot notation to access the elements of an array and to access the fields of a subdocument.
To access an element of an array by the zero-based index position, concatenate the array name with the dot (.) and zerobased index position, and enclose in quotes:
'<array>.<index>’
To access a field of a subdocument with dot-notation, concatenate the subdocument name with the dot (.) and the field
name, and enclose in quotes:
'<subdocument>.<field>’
12. Query Operators
•
Select All Documents in a Collection
An empty query document ({}) selects all documents in the collection:
db.inventory.find( {} )
•
Specify Equality Condition
To specify equality condition, use the query document { <field>: <value> } to select all documents that contain the<field>
with the specified <value>.
The following example retrieves from the inventory collection all documents where the type field has the value snacks:
db.inventory.find( { type: "snacks" } )
•
Specify Conditions Using Query Operators
A query document can use the query operators to specify conditions in a MongoDB query.
The following example selects all documents in the inventory collection where the value of the type field is either 'food'
or 'snacks':
db.inventory.find( { type: { $in: [ 'food', 'snacks' ] } } )
Although you can express this query using the $or operator, use the $in operator rather than the $or operator when
performing equality checks on the same field.
13. 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. In the following example, the query document specifies an equality match on the field food and a less
than ($lt) comparison match on the field price:
db.inventory.find( { type: 'food', price: { $lt: 9.95 } } )
•
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. In the following example, the query
document selects all documents in the collection where the field qty has a value greater than ($gt) 100 or the value of the
price field is less than ($lt) 9.95:
db.inventory.find(
{ $or: [
{ qty: { $gt: 100 } },
{ price: { $lt: 9.95 } }
]
}
)
• Specify AND as well as OR Conditions
With additional clauses, you can specify precise conditions for matching documents.
In the following example, the compound query document selects all documents in the collection where the value of the
type field is 'food' and either the qty has a value greater than ($gt) 100 or the value of the price field is less than ($lt) 9.95:
db.inventory.find( { type: 'food', $or: [ { qty: { $gt: 100 } },
{ price: { $lt: 9.95 } } ]
})
14. Query Operators
Subdocuments
When the field holds an embedded document (i.e. subdocument), you can either specify the entire subdocument as the
value of a field, or “reach into” the subdocument using dot notation, to specify values for individual fields in the
subdocument:
•
Exact Match on Subdocument
To specify an equality match on the whole subdocument, use the query document { <field>: <value> } where <value> is the
subdocument to match. Equality matches on a subdocument require that the subdocument field match exactly the
specified<value>, including the field order. In the following example, the query matches all documents where the value of
the field producer is a subdocument that containsonly the field company with the value 'ABC123' and the field address
with the value '123 Street', in the exact order:
db.inventory.find(
{
producer: {
company: 'ABC123',
address: '123 Street'
}
}
)
• Equality Match on Fields within Subdocument
Equality matches for specific fields within subdocuments select the documents in the collection when the field in the
subdocument contains a field that matches the specified value. In the following example, the query uses the dot notation
to match all documents where the value of the field producer is a subdocument that contains a field company with the
value 'ABC123' and may contain other fields:
db.inventory.find( { 'producer.company': 'ABC123' } )
15. Query Operators
Arrays
When the field holds an array, you can query for an exact array match or for specific values in the array. If the array holds
sub-documents, you can query for specific fields within the sub-documents using dot notation:
•
Exact Match on an Array
To specify equality match on an array, use the query document { <field>: <value> } where <value> is the array to match.
Equality matches on the array require that the array field match exactly the specified <value>, including the element order.
In the following example, the query matches all documents where the value of the field tags is an array that holds exactly
three elements, 'fruit', 'food', and 'citrus', in this order:
db.inventory.find( { tags: [ 'fruit', 'food', 'citrus' ] } )
•
Match an Array Element
Equality matches can specify a single element in the array to match. These specifications match if the array contains at least
oneelement with the specified value.
In the following example, the query matches all documents where the value of the field tags is an array that contains 'fruit'
as one of its elements:
db.inventory.find( { tags: 'fruit' } )
•
Match a Specific Element of an Array
Equality matches can specify equality matches for an element at a particular index or position of the array.
In the following example, the query uses the dot notation to match all documents where the value of the tags field is an
array whose first element equals 'fruit':
db.inventory.find( { 'tags.0' : 'fruit' } )
16. Query Operators
Query Selectors
•
Comparison
Name
$gt
$gte
$in
$lt
$lte
$ne
$nin
•
Description
Matches values that are greater than the value specified in the query.
Matches values that are equal to or greater than the value specified in the query.
Matches any of the values that exist in an array specified in the query.
Matches values that are less than the value specified in the query.
Matches values that are less than or equal to the value specified in the query.
Matches all values that are not equal to the value specified in the query.
Matches values that do not exist in an array specified to the query.
Logical
Name
$or
$and
$not
$nor
Description
Joins query clauses with a logical OR returns all documents that match the conditions of either
clause.
Joins query clauses with a logical AND returns all documents that match the conditions of both
clauses.
Inverts the effect of a query expression and returns documents that do not match the query
expression.
Joins query clauses with a logical NOR returns all documents that fail to match both clauses.
17. Query Operators and Projection Operators
Query Selectors
•
Evaluation
Name
$mod
$regex
$where
•
Description
Performs a modulo operation on the value of a field and selects documents with a
specified result.
Selects documents where values match a specified regular expression.
Matches documents that satisfy a JavaScript expression.
Array
Name
$all
$elemMatch
$size
Description
Matches arrays that contain all elements specified in the query.
Selects documents if element in the array field matches all the specified
$elemMatch condition.
Returns documents if the array field is a specified size.
18. Query Operators and Projection Operators
Query Selectors
•
Geospatial
Name
$geoWithin
$geoIntersects
$near
$nearSphere
•
Description
Selects geometries within a bounding GeoJSON geometry.
Selects geometries that intersect with a GeoJSON geometry.
eturns geospatial objects in proximity to a point.
Returns geospatial objects in proximity to a point on a sphere.
Projection Operators
Name
$
$elemMatch
$slice
Description
Projects the first element in an array that matches the query condition.
Projects only the first element from an array that matches the specified $elemMatch
condition.
Limits the number of elements projected from an array.
Supports skip and limit slices.
19. Update Documents
• Update Operators
Fields
Name
$inc
$rename
$setOnInsert
$set
$unset
Description
Increments the value of the field by the specified amount.
Renames a field.
Sets the value of a field upon document creation during an upsert. Has no effect on
update operations that modify existing documents.
Sets the value of a field in an existing document.
Removes the specified field from an existing document.
20. Update Documents
•
Update Function
db.collection.update( <query>, <update>, { upsert: <boolean>, multi: <boolean> } )
Sample
db.books.update( { item: "Divine Comedy" }, {
$set: { price: 18 },
$inc: { stock: 5 } } )
• Add New Fields
If the <update> parameter contains fields not currently in the document, the update() method adds
the new fields to the document. The following operation adds two new fields: mbranch and aka.
The operation adds aka in the subdocument name:
db.bios.update( { _id: 3 }, { $set: { mbranch: "Navy”, "name.aka": "Amazing Grace” } } )
•
Remove Fields
The following operation uses the $unset operator to remove the birth field from the document that
has _id equal to 3:
db.bios.update( { _id: 3 }, { $unset: { birth: 1 } } )
21. Update Documents
• Insert a New Document if No Match Exists (Upsert)
The following command sets the upsert option to true so that update() creates a new document in the
books collection if no document matches the <query> parameter:
db.books.update( { item: "The New Life" },
{ item: "The New Life",
author: "Dante",
price: 15 },
{ upsert: true }
)
• Update Multiple Documents
To update multiple documents, set the multi option to true. The following example queries the bios
collection for all documents where awards.award is set to Turing. The update sets the turing field to true:
db.bios.update(
{ "awards.award": "Turing" },
{ $set: { turing: true } },
{ multi: true }
)
• Combine the Upsert and Multi Parameters
db.books.update( { author: "Dante" },
{ $set: { born: "Florence", died: "Ravenna" } },
{ upsert: true, multi: true }
)
22. Update Arrays
•
Operators
Name
$
update.
$addToSet
$pop
$pullAll
$pull
$pushAll
$push
•
Description
Acts as a placeholder to update the first element that matches the query condition in an
Adds elements to an existing array only if they do not already exist in the set.
Removes the first or last item of an array.
Removes all matching values from an array.
Removes items from an array that match a query statement.
Deprecated. Adds several items to an array.
Adds an item to an array.
Modifiers
Name
$each
$slice
$sort
Description
Modifies the $push and $addToSet operators to append multiple items for array updates.
Modifies the $push operator to limit the size of updated arrays.
Modifies the $push operator to reorder documents stored in an array.
More @ http://docs.mongodb.org/manual/reference/operator/update/
23. Update Arrays
• Update an Element by Position
If the update operation requires an update of an element in an array field, the update() method can
perform the update using the position of the element and dot notation. Arrays in MongoDB are zerobased.
The following operation queries the bios collection for the first document with _id field equal to 1 and
updates the second element in the contribs array:
db.bios.update(
{ _id: 1 },
{ $set: { "contribs.1": "ALGOL 58" } }
)
• Update an Element if Position is Unknown
If the position in the array is not known, the update() method can perform the update using the $
positional operator. The array field must appear in the <query> parameter in order to determine which
array element to update.
The following operation queries the bios collection for the first document where the _id field equals 3 and
the contribs array contains an element equal to compiler. If found, the update() method updates the first
matching element in the array to A compiler in the document:
db.bios.update(
{ _id: 3, "contribs": "compiler" },
{ $set: { "contribs.$": "A compiler" } }
)
24. Update Arrays
• Update a Document Element
The update() method can perform the update of an array that contains subdocuments by using the
positional operator (i.e. $) and the dot notation.
The following operation queries the bios collection for the first document where the _id field equals 6 and
the awards array contains a subdocument element with the by field equal to ACM. If found, the update()
method updates the by field in the first matching subdocument:
db.bios.update(
{ _id: 6, "awards.by": "ACM" } ,
{ $set: { "awards.$.by": "Association for Computing Machinery" } }
)
• Add an Element
The following operation queries the bios collection for the first document that has an _id field equal to 1
and adds a new element to the awards field:
db.bios.update(
{ _id: 1 },
{
$push: { awards: { award: "IBM Fellow", year: 1963, by: "IBM" } }
}
)
25. Mongo Shell Methods
JavaScript in MongoDB Although these methods use JavaScript, most interactions with MongoDB do not use JavaScript but use an
idiomatic driver in the language of the interacting application.
Collection Functions
Name
Description
db.collection.aggregate()
Provides access to the aggregation pipeline.
db.collection.distinct()
Returns an array of documents that have distinct values for the specified field.
db.collection.drop()
Removes the specified collection from the database.
db.collection.dropIndex()
Removes a specified index on a collection.
db.collection.dropIndexes()
Removes all indexes on a collection.
db.collection.ensureIndex()
Creates an index if it does not currently exist. If the index existsensureIndex() does
nothing.
db.collection.find()
Performs a query on a collection and returns a cursor object.
db.collection.findAndModify()
Atomically modifies and returns a single document.
db.collection.findOne()
Performs a query and returns a single document.
db.collection.getIndexes()
Returns an array of documents that describe the existing indexes on a collection.
db.collection.group()
Provides simple data aggregation function. Groups documents in a collection by a key, and
processes the results. Use aggregate() for more complex data aggregation.
db.collection.insert()
Creates a new document in a collection.
db.collection.mapReduce()
erforms map-reduce style data aggregation.
db.collection.reIndex()
Rebuilds all existing indexes on a collection.
db.collection.remove()
Deletes documents from a collection.
db.collection.renameCollection()
Changes the name of a collection.
db.collection.save()
Provides a wrapper around an insert() and update() to insert new documents.
db.collection.update()
Modifies a document in a collection.
26. Mongo Shell Methods
Cursor Functions
Name
Description
cursor.count()
Returns a count of the documents in a cursor.
cursor.explain()
Reports on the query execution plan, including index use, for a cursor.
cursor.hint()
Forces MongoDB to use a specific index for a query.
cursor.limit()
Constrains the size of a cursor’s result set.
cursor.map()
Applies a function to each document in a cursor and collects the return values in an array.
cursor.max()
Specifies an exclusive upper index bound for a cursor. For use with cursor.hint()
cursor.min()
Specifies an inclusive lower index bound for a cursor. For use with cursor.hint()
cursor.next()
Returns the next document in a cursor.
cursor.size()
Returns a count of the documents in the cursor after applying skip() and limit()methods.
cursor.skip()
Returns a cursor that begins returning results only after passing or skipping a number
of documents.
cursor.sort()
Returns results ordered according to a sort specification.
cursor.toArray()
Returns an array that contains all documents returned by the cursor.
More shell functions @ http://docs.mongodb.org/manual/reference/method/