Escolar Documentos
Profissional Documentos
Cultura Documentos
Document Database
DOCUMENT ORIENTED
DATABASE
store data in the form of documents
document can be in a JSON, BSON, XML, YAML, etc format
Documents are retrieved from the database using unique key that
represents that document.
key may be a simple string, URI, or a path
Support data sharding
MONGODB
DATABASE
physical container for collections
Each database gets its own set of files on the file system. A single
MongoDB server typically has multiple databases.
COLLECTION
group of MongoDB documents
equivalent of an RDBMS table
A collection exists within a single database
Collections do not enforce a schema.
Documents within a collection can have different fields.
Typically, all documents in a collection are of similar or related
purpose
DOCUMENT
set of key-value pairs. Documents have dynamic schema. Dynamic
schema means that documents in the same collection do not need
to have the same set of fields or structure, and common fields in a
collection's documents may hold different types of data
RDBMS VS MONGODB
RDBMS
MongoDB
Database
Database
Table
Collection
Tuple/Row
Document
Column
Field
Table Join
Embedded Documents
Primary Key
SAMPLE DOCUMENT
12 bytes hexadecimal
number
Assures the uniqueness of
every document
ID
1
Current timestamp
Machine ID
MongoDB
server
process
ID
10
11
12
Simple
Incremental
value
MONGODB ENVIRONMENT
Download from
https://www.mongodb.com/
MongoDB starts
2016-06-30T11:53:43.521+0530 I NETWORK [initandlisten] waiting for
connections on port 27017
2016-06-30T11:57:16.861+0530 I NETWORK [initandlisten] connection accepted
from 127.0.0.1:51595 #1 (1 connection now open)
COMMANDS
Command
Description
Example
show dbs
show dbs
use db_name
use mydb
db
db
db.dropDatabase()
Delete database
db.createCollection("mycolle
ction")
show collections
show collections
db.coll_name.insert(
{key:value})
db.coll_name.drop()
db.movie.drop()
CREATE COLLECTIONS
(OPTIONS)
Field
Type
Description
capped
Boolean
autoIndex
ID
Boolean
size
number
max
number
CAPPED COLLECTIONS
to store data in the same order it is inserted
fixed size
high-performance
"auto-FIFO age-Out
can't remove an individual object from the capped collection
maximum size for a capped collection is 1e9(i.e. 1X109) for 32 bit
machines. For 64 bit machines, there is no theoretical limit.
Practically, it can be extended till your system resources permit.
can be used for logging, caching and auto archiving
Command
Description
Example
>
db.tweet.insert({id:1,location:"coimbatore",tw
eet:"kjdhdskjh"})
WriteResult({ "nInserted" : 1 })
>
db.tweet.insert({id:2,location:"coimbatore",tw
eet:"kjdhdskjh"},{id:3})
db.COLLECTION_NAM find()
E.find()
display all the
documents in a non
structured way.
db.tweet.find().pretty()
{
"_id" :
ObjectId("577a22bfe94a2882699e6b7a"),
"id" : 1,
"location" : "coimbatore",
"tweet" : "kjdhdskjh"
}
{
"_id" :
ObjectId("577a22dee94a2882699e6b7b"),
"id" : 2,
"location" : "coimbatore",
"tweet" : "kjdhdskjh"
}
pretty()
display the results in a
formatted way
Operation
Syntax
Example
RDBMS Equivalent
Equality
{<key>:<value>}
db.mycol.find({"by":"tutorials
point"}).pretty()
where by = 'tutorials
point'
Less Than
{<key>:
{$lt:<value>}}
db.mycol.find({"likes":
{$lt:50}}).pretty()
Less Than
Equals
{<key>:
{$lte:<value>}}
db.mycol.find({"likes":
{$lte:50}}).pretty()
Greater Than
{<key>:
{$gt:<value>}}
db.mycol.find({"likes":
{$gt:50}}).pretty()
Greater Than
Equals
{<key>:
{$gte:<value>}}
db.mycol.find({"likes":
{$gte:50}}).pretty()
Not Equals
{<key>:
{$ne:<value>}}
db.mycol.find({"likes":
{$ne:50}}).pretty()
where likes != 50
OR IN MONGODB
Syntax
Example
>db.mycol.find({$or:[{"by":"tutorials point"},{"title": "MongoDB
Overview"}]}).pretty()
>db.mycol.find(
{
"_id": ObjectId(7df78ad8902c),
$or: [
"url": "http://www.tutorialspoint.com",
).pretty()
"likes": "100"
}
UPDATE() METHOD
>db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA,
UPDATED_DATA)
>db.mycol.update({'title':'MongoDB Overview'},
{$set:{'title':'New MongoDB Tutorial'}},{multi:true})
>db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})
>db.mycol.save(
{
"_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials
Point Overview",
"by":"Tutorials Point"
}
)
REMOVE() METHOD
>db.COLLECTION_NAME.remove(DELETION_CRITERIA)
>db.mycol.remove({'title':'MongoDB Overview'})
Delete only first record
>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
Remove All documents
>db.mycol.remove()
MONGODB
DATATYPES
Type
Description
Number
Double
String
Object
Array
Binary data
Object id
Boolean
Date
Null
10
Regular
Expression
11
JavaScript
13
Symbol
Not supported by the shell. If the shell gets a symbol from the database, it
14
Type
Description
JavaScript
(with
scope)
Number
15
32-bit
integer
Timestam
p
64-bit
integer
18
Min key
255
Max key
127
http://www.w3resource.com/mongodb/mongodb-data-types.php
http://www.tutorialspoint.com/mongodb/mongodb_datatype.htm