MongoDB Lecture 1
MongoDB Lecture 1
MONGODB
by
Manya Gidwani
Assistant Professor
IT DepartmentSAKEC
CONTENTS
Introduction to MongoDB
Document Database
DataTypes in MongoDB
? MongoDB is a cross-platform, document
oriented database that provides, high
performance, high availability, and easy
scalability. MongoDB works on concept
of collection and document.
? Database is a 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 is a group of MongoDB documents.
? It is the equivalent of an RDBMS table.
? 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.
? A document is a 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 MongoDB
Database Database
Table Collection
Tuple/Row Document
column Field
Unstructured, or structured
Data Structure data that will potentially Structured
grow fast
Atomic Transactions
Doesn’t fully support all
(e.g., having several Supports atomic
operations but supports
operations within a transactions
multi-document transactions
transaction)
DOCUMENT DATABASE
? A document database (also known as a document-oriented
database or a document store) is a database that stores
information in documents.
? A document is a record in a document database. A document
typically stores information about one object and any of its
related metadata.
? Documents store data in field-value pairs. The values can be a
variety of types and structures, including strings, numbers,
dates, arrays, or objects. Documents can be stored in formats
like JSON, BSON, and XML.
DOCUMENT DATABASE
? A collection is a group of documents.
Collections typically store documents that
have similar contents.
? Not all documents in a collection are required
to have the same fields, because document
databases have a flexible schema. Note that
some document databases provide schema
validation, so the schema can optionally be
locked down when needed.
DATATYPES IN MONGODB
? MongoDB supports many data types whose list is given below:
? String : This is the most commonly used datatype to store the data.
String in MongoDB must be UTF-8 valid.
db.TestCollection.insert({"string data type" : "This is a sample
message."})
? Integer :This type is used to store a numerical value. Integer can be 32
bit or 64 bit depending upon your server.
db.TestCollection.insert({"Integer example": 62})
? Boolean : This type is used to store a boolean (true/ false) value.
db.TestCollection.insert({"Nationality Indian": true})
? Double : This type is used to store floating point values.
? db.TestCollection.insert({"double data type": 3.1415})
? Arrays :This type is used to store arrays or list or multiple values into
one key.
var degrees = ["BCA", "BS", "MCA"]
db.TestCollection.insert({" Array Example" : " Here is an example of
array", " Qualification" : degrees})
DATATYPES IN MONGODB
? Min/ Max keys : This type is used to compare a value against the
lowest and highest BSON elements.
? 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.
var date=new Date()
var date2=ISODate()
var month=date2.getMonth()
db.TestCollection.insert ({"Date":date, "Date2":date2,
"Month":month})
? Timestamp : ctimestamp. This can be handy for recording when a
document has been modified or added.
? Object : This datatype is used for embedded documents.
Create Collection
db.createCollection(name, options)
>db.createCollection("mycollection")
{ "ok" : 1 }
>show collections mycollection system.indexes
Insert Records
? >db.movie.insert({"name":"tutorials point"})
? >show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
_id is 12 bytes hexadecimal number unique for
every document in a collection.
? To insert multiple documents in single query,
you can pass an array of documents in insert()
command.
? The save() method replaces the existing document
with the new document passed in save() method
? Syntax
>db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DA
TA})
? The remove() Method is used to remove
document from the collection. remove() method
accepts two parameters. One is deletion criteria and
second
Thank
You