Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

MONGODB

No description
by

Mona Sharma

on 22 November 2016

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of MONGODB

AGENDA
MongoDB is a collection-oriented, schema-free document database.
By collection-oriented, we mean that data is grouped into sets that are called 'collections'. Each collection has a unique name in the database, and can contain an unlimited number of documents.
By schema-free, we mean that the database doesn't need to know anything about the structure of the documents that you store in a collection. In fact, you can store documents with different structure in the same collection if you so choose.
By document, we mean that we store data that is a structured Format "BSON" which are similar to JSON object.Collection of key-value pairs, where keys are strings, and values are any of a rich set of data types, including arrays and documents. Example:
What is MONGO DB?
Create two documents named j and k by using the following sequence of JavaScript operations:
j = { name : "mongo" }
k = { x : 3 }
Insert the j and k documents into the testData collection with the following sequence of operations:
db.testData.insert( j )
db.testData.insert( k )
When you insert the first document, the mongod will create the collection if the specified collection is not present.
Confirm that the documents exist in the testData collection by issuing a query on the collection using the find() method:
Creating and Inserting a Document in Mongodb
MONGODB
To start MongoDB, run mongod.exe. For example, from the Command Prompt:
C:\Program Files\MongoDB\bin\mongod.exe

This starts the main MongoDB database process. The waiting for connections message in the console output indicates that the mongod.exe process is running successfully.
To connect to MongoDB through the mongo.exe shell, open another Command Prompt and run mongo.exe

After starting the mongo shell your session will use the test database by default.
At any time, issue the following operation at the mongo to report the name of the current database:
db
From the mongo shell, display the list of databases, with the following operation:
show dbs
Switch to a new database named mydb, with the following operation:
use mydb
Installing MongoDB

Creating local directory
Queries in Mongo Db:
• What is Mongo database ?
• Key features/Advantages over RDBMS databases.
• MongoDB Crud Operations
Quiz
• Spring and Mongo DB
• Replication
• Sharding
• Aggregation
• Indexes

MongoDB requires a data directory to store all data. MongoDB’s default data directory path is \data\db.

Create this folder using the following commands from a Command Prompt:
md\data\db
You can specify an alternate path for data files using the --dbpath option to mongod.exe, for example:
C:\mongodb\bin\mongod.exe --dbpath d:\test\mongodb\data
UPDATE ,SAVE AND DELETE QUERY
Quick QUERY QUIZ???
SELECT * FROM users WHERE age>33


SELECT * FROM users WHERE a=1 and b='q'
SELECT * FROM users WHERE a=1 or b=2

SELECT * FROM foo WHERE name=‘bob’ and (a=1 or b=2 )
SELECT * FROM users WHERE age>33 AND age<=40

LIMIT AND SORT in MONGODB
AGGREGATE METHODS:
OBJECT ID:
An ObjectId is a 12-byte BSON type having the following structure:

The first 4 bytes representing the seconds
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
Creating New ObjectId:
>newObjectId = ObjectId()
Instead of MongoDB generating the ObjectId, you can also provide a 12-byte id:
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")


MONGODB JAVA AND SPRING
You need to download the jar from the path Download mongo.jar. Make sure to download latest release of it.
You need to include the mongo.jar into your classpath.
You can do following operations:
Connecting to the database.
Retrieving a document.
Updating a document .
Deleting a document.
Advantage Over RDBMS
Automatic Scaling and Shrading
Automatic sharding (auto-partitioning of data across servers)
Reads and writes are distributed over shards.
No joins or multi-document transactions make distributed queries easy and fast.
Database systems with large data sets and high throughput applications can challenge the capacity of a single server. High query rates can exhaust the CPU capacity of the server.

Sharding, or horizontal scaling, divides the data set and distributes the data over multiple servers, or shards.
REPLICATION
A replica set is a group of mongod instances that host the same data set.
One mongod, the primary, receives all write operations. Replica set can have only one primary.

If the primary is unavailable, the replica set will elect a secondary to be primary.
By default, clients read from the primary, however, clients can specify a read preferences to send read operations to secondaries.
To support replication, the primary logs all changes to its data sets in its

oplog
.
The
oplog
(operations log) is a special capped collection that keeps a rolling record of all operations that modify the data stored in your databases.

MongoDB applies database operations on the primary and then records the operations on the primary’s oplog. The secondary members then copy and apply these operations in an asynchronous process.

All replica set members contain a copy of the oplog, in the
local.oplog.rs
collection, which allows them to maintain the current state of the database.

To facilitate replication, all replica set members send heartbeats (pings) to all other members. Any member can import oplog entries from any other member

INDEXES
Fundamentally, indexes in MongoDB are similar to indexes in other database systems. MongoDB defines indexes at the collection level and supports indexes on any field or sub-field of the documents in a MongoDB collection.
Without indexes, MongoDB must scan every document in a collection to select those documents that match the query statement.
MONGODB AND RDBMS
Aggregation
Aggregations are operations that process data records and return computed results.

MongoDB 2.2 introduced a new aggregation framework, modeled on the concept of data processing pipelines
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.
OR and AND QUERY
MAP REDUCE
MongoDB also provides map-reduce operations to perform aggregation. In general, map-reduce operations have two phases:
• a map stage that processes each document and emits one or more objects for each
input document

• reduce phase that combines the output of the map operation.
Optionally, map-reduce can have a finalize stage to make final modifications to the result. Like other aggregation operations, map-reduce can specify a query condition to select the input documents as well as sort and limit the results.

Database References
MongoDB applications use one of two methods for relating documents:

Manual references where you save the _id field of one document in another document as a reference. Then your application can run a second query to return the related data. These references are simple and sufficient for most use cases.
original_id = ObjectId()

db.places.insert({
"_id": original_id,
"name": "Broadway Center",
"url": "bc.example.net"
})

db.people.insert({
"name": "Erin",
"places_id": original_id,
"url": "bc.example.net/Erin"
})
DBRefs are references from one document to another using the value of the first document’s _id field, collection name, and, optionally, its database name. By including these names, DBRefs allow documents located in multiple collections to be more easily linked with documents from a single collection.
{ "$ref" : <value>, "$id" : <value>, "$db" : <value> }
{
“_id” : ObjectId(“528ba7691738025d11aab772”),
“manufacturer” : “Porsche”,
“name” : “550 Spyder”,
“category” : {
“sports”,
“touring”,
“coupé”
}
}

PK | Name | Manufacturer | categories
123 | “550 Spyder” | “Porsche|“sports,touring,race,coupé”

Vehicle_Id | Name
—————————
2253 | “”550 Spyder”

Vehicle_Id | Category
——————————-
2253 | “sports”
2253 | “touring”
2253 | “coupé”
Vehicle_Id | Manufacturer
————————————-
2253 | “Porsche AG”


Many to Many relationship example:


var party = { _id: "chessparty"
, name: "Chess Party!"
, attendees: ["seanhess", "bob"] }

var user = { _id: "seanhess", name: "Sean Hess", events: ["chessparty"]}


Example
Server-side JavaScript execution
JavaScript can be used in queries, aggregation functions (such as MapReduce), and sent directly to the database to be executed.
Full transcript