MongoDB is an open-source document database, and the leading NoSQL database. Written in C++.
MongoDB has official drivers for a variety of popular programming languages and development environments. There are also a large number of unofficial or community-supported drivers for other programming languages and frameworks.
2. Introduction to NoSQL
What is NoSQL?
It’s a whole new way of thinking about a database and encompasses a wide variety of
different database technologies.
A non-relational and largely distributed database system that enables rapid and analysis
of extremely high-volume, disparate (different) data types.
Why NoSQL?
The Benefits of NoSQL : A very flexible and schema-less data model, horizontal
scalability, distributed architectures.
• Dynamic Schemas
• Auto-sharding
• Replication
• Integrated Caching
3. Document databases pair each key with a complex data structure
known as a document. Documents can contain many different key-value pairs,
or key-array pairs, or even nested documents.
Graph stores are used to store information about networks, such as
social connections. Graph stores include Neo4J and HyperGraphDB.
Key-value stores are the simplest NoSQL databases. Every single
item in the database is stored as an attribute name (or "key"), together with
its value. Examples of key-value stores are Riak and Voldemort. Some key-
value stores, such as Redis, allow each value to have a type, such as "integer",
which adds functionality.
Wide-column stores such as Cassandra and HBase are optimized
for queries over large datasets, and store columns of data together, instead of
rows
4. SQL Database NoSQL Database
Types One type (SQL database) with
minor variations
Many different types including
key-value stores, document
databases, wide-column stores,
and graph databases
Development History Developed in 1970s to deal
with first wave of data storage
applications.
Developed in 2000s to deal with
limitations of SQL databases,
particularly concerning scale,
replication and unstructured
data storage.
Examples MySQL, Postgres, Oracle
Database
MongoDB, Cassandra, HBase,
Neo4j ,Riak, Voldemort,
CouchDB ,DynamoDB
Schemas Structure and data types are
fixed in advance.
Typically dynamic. Records can
add new information on the fly,
and unlike SQL table
Scaling Vertically Horizontally
Data Manipulation Specific language using Select,
Insert, and Update statements,
e.g. SELECT fields FROM table
WHERE…
Through object-oriented APIs
5. Relational and NoSQL data models are very different. The relational model takes data
and separates it into many interrelated tables that contain rows and columns. Tables
reference each other through foreign keys that are stored in columns as well.
NoSQL databases have a very different model. For example, a document-oriented NoSQL
database takes the data you want to store and aggregates it into documents using the
JSON format.
6. History:
Development of MongoDB began in October 2007 by 10gen.
The first public release was in February 2009.
What is MongoDB
MongoDB is an open-source document database that provides
• High performance
• High availability
• Automatic scaling and sharding
MongoDb is a Open Source database written in C++.
Drivers and client libraries are typically written in their respective languages, although
some drivers use C extensions for better performance.
MongoDB does not support SQL It supports a rich, ad-hoc query language of its own.
MongoDb stores data as documents. So it is a document oriented database.
7. Document Database
A record in MongoDB is a document, which is a data structure composed of field and
value pairs. MongoDB documents are similar to JSON objects. The values of fields may
include other documents, arrays, and arrays of documents.
Licensing and support
MongoDB is available for free under the GNU Affero General Public License.
he language drivers are available under an Apache License. In addition, MongoDB Inc.
offers commercial licenses for MongoDB.
Installation :
To install the MongoDB on windows or Linux , first download the latest release of
MongoDB from
http://www.mongodb.org/downloads
8. Create Database
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.
To check your currently selected database use the command- db
If you want to check your databases list, then use the command- show dbs.
MongoDB db.dropDatabase() command is used to drop a existing database.
9. Create Collection
MongoDB db.createCollection(name, options) is used to create collection.
In the command, name is name of collection to be created. Options is a
document and used to specify configuration of collection
10.
11. INSERT & FIND OPERATION
To insert data into MongoDB collection, you need to use MongoDB's insert() method.
Syntex :
db.COLLECTION_NAME.insert(document)
12. $set operator to insert or update the document
Syntex :
db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA,
UPDATED_DATA)
db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New
MongoDB Tutorial'}
db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New
MongoDB Tutorial'}},{multi:true})
13. The remove() Method
MongoDB's remove() method is used to remove document from the
collection. remove() method accepts two parameters. One is deletion
criteria and second is justOne flag
Syntex:
db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)
db.mycol.remove({'title':'MongoDB Overview'})
Notas do Editor
For more information about nosql :http://www.couchbase.com/nosql-resources/what-is-no-sql
JSON- JavaScript Object Notation
Voldemort is a distributed key-value storage system, It is used at ’LinkedIn’ by numerous critical services powering a large portion of the site.
Riak uses a simple key/value model for object storage. You can store anything you want in Riak: text, images, JSON/XML/HTML documents, user and session data, backups, log files, an
MongoDB uses a document mode.
Drivers and client libraries are typically written in their respective languages, although some drivers use C extensions for better performance.
MongoDB has no configurable cache. MongoDB uses all free memory on the system automatically by way of memory-mapped files.
Casbah is an interface for MongoDB designed to provide more flexible access from both Java and Scala. The core focus is on providing a Scala oriented wrapper interface around the Java mongo driver.
MongoDB in production : Craiglist, FourSquare, Shutterfly, bit.ly, spike.com, Intuit, The New York Times, sourceforge.net
JSON or JavaScript Object Notation, is an open standard format that uses human-readable text to transmit data objects consisting of attribute–value pairs. It is used primarily to transmit data between a server and web application, as an alternative to XML.
If load increases (more storage space, more processing power), it can be distributed to other nodes across computer networks. This is called as sharding.
MongoDb supports Map/Reduce framework for batch processing of data and aggregation operation.
Basic syntax of use DATABASE statement is as follows: use DATABASE_NAME
Syntax of dropDatabase() command is as follows:
db.dropDatabase()
Basic syntax of createCollection() method is as follows
db.createCollection(name, options)
Name : String type, Option : (Document type) - (Optional) Specify options about memory size and indexing
Syntax of drop() command is as follows ;db.COLLECTION_NAME.drop()
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 Nosql database', by: Prashant', url: 'http://www.myStudyPrashant.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 500 })
MongoDB supports javascript ;
j = { name : "mongo" } k = { x : 3 }
k = { x : 3 }
db.testData.insert( j ) db.testData.insert( k )
_id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 3 bytes incrementer)ObjectId : _id - is 12 bytes BSON hexadecimal number unique for every document in a collection. 12 bytes are divided as follows:
The first 4 bytes representing the seconds since the unix epoch
The next 3 bytes are the machine identifier
The next 2 bytes consists of process id
The last 3 bytes are a random counter value
By default mongodb will update only single document, to update multiple you need to set a paramter 'multi' to true.
If there are multiple records and you want to delete only first record, then set justOne parameter inremove() method
db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
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()