Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

Mongo DB

About Mongo DB
 Mongo DB is an Open source database
 No Sql Database
 It is written in C++
 MongoDB is a cross-platform, document oriented database that provides
 MongoDB works on concept of collection and document.
 Collection is a group of MongoDB documents
 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

Table Join Embedded Documents

Primary Key Primary Key (Default key _id provided)


 _id is a 12 bytes hexadecimal number which assures the uniqueness of every document.

These 12 bytes

 First 4 bytes Current Time stamp


 Next 3 bytes machine id
 Next 2 bytes process id for Mongo DB server
 Remaining 3 bytes incremental values
 You can provide _id while inserting the document
 If you don’t provide then MongoDB provides a unique id for every document
 While in MongoDB, there is no concept of relationship with in collection.

Advantages

Schema less − MongoDB is a document database in which one collection holds different
documents. Number of fields, content and size of the document can differ from one document
to another.

 Document Oriented Storage − Data is stored in the form of JSON style documents.
 The default location for the MongoDB data directory is c:\data\db.

Mongod – Server

Mongo - Client

 To get a list of commands, type db.help() in MongoDB client


 To get stats about MongoDB server, type the command db.stats() in MongoDB client
 Default databases test

Where use to MongoDB

 Big Data

 Content Management and Delivery

 Mobile and Social Infrastructure

 User Data Management

 Data Hub
 Mongo DB will support Many data types

Examples
String, Integer, Boolean, Double, Min/ Max keys, Arrays, Timestamp, Object, Null, Symbol,
Date, Object ID, Binary data, Code and Regular expression
Queries
1 – To Create Data Base
Syntax
use database_name
Example
>use techmdb

2 – Show Data Bases


>show dbs

Note: If db don’t have any collections, it will not show in db list.


3 – Drop Data bases
>db.dropDatabase()
Note: If we have not selected any data base. Default data base will be deleted (test)
4 – Create Collections
Syntax
db.createCollection(name)
(Or)
db.createCollection(name, options)

Parameter Type Description

Name String Name of the collection to be created

Options Document (Optional) Specify options about memory


size and indexing

Syntax - Option parameter


db.createCollection(name, { capped : true, autoIndexID : true, size : 6142800, max : 10000 })

Field Type Description

(Optional) If true, enables a capped collection. Capped collection is a


fixed size collection that automatically overwrites its oldest entries when
capped Boolean
it reaches its maximum size. If you specify true, you need to specify
size parameter also.

(Optional) If true, automatically create index on _id field.s Default value


autoIndexID Boolean
is false.

(Optional) Specifies a maximum size in bytes for a capped collection. If


size number
capped is true, then you need to specify this field also.

(Optional) Specifies the maximum number of documents allowed in the


max number
capped collection.
Example
1 – Only with Collection Name
> db.createCollection("techmcolls1");
> db.createCollection("techmcolls2");

2 – Now Collection name and Options (Document configuration)


> db.createCollection("techmcolls3",{capped : true, autoIndexID : true, size : 6142800, max :
10000});
> db.createCollection("techmcolls4",{capped : true, autoIndexID : true, size : 6142800, max :
10000});

5 – Show Collections List in selected Data Base


Examples
> show collections;

Note: While inserting the document, if the collection doesn’t exist, Mongo DB will create the
Collection Automatically, the will insert the document
6 – Drop the Collections
Syntax
db.collection_name.drop();
Example
> db.techmcolls3.drop();

7 – Create Documents

1 - To insert data into MongoDB collection, you need to use MongoDB's insert () or
save () method.

Example

> db.techmcolls1.insert({
emp_name:"employee1",
emp_id:"techm1001",
emp_age:25,
emp_dob: new Date(1990,03,17),
emp_skils:["Java", "J2EE", "Spring", "hibernate","MySql","MongoDB"],
emp_address:[{
address1 : "Chennai",
address2: "Bangalore",
address3 : "Pune"
}]
});
Now we can add one more field emp_band and address4

db.techmcolls1.insert({
emp_name:"employee2",
emp_id:"techm1002",
emp_age:26,
emp_dob: new Date(1991,04,18),
emp_band :"U3",
emp_skils:["JavaScript", "CSS","bootstrap","Jquery","AngularJS","MongoDB"],
emp_address:[{
address1 : "Madiwala",
address2: "Hinjewadi",
address3 : "Coimbatore",
address4:"Salem"
}]
});

2 - While inserting the document, if the collection doesn't exist in the database, then MongoDB
will create this collection and then insert a document into it.

Examples

Here the collection of techmcolls5 doesn’t exist in list, but while we inserting, Collection created
and document inserted successfully
3 - In the inserted document, if we don't specify the _id parameter, then MongoDB assigns a
unique ObjectId for this document.

4 - To insert multiple documents in a single query, you can pass an array of documents in insert ()
command.

And we have added one more field emp_band in second document of in these arrays
db.techmcolls2.find().pretty();
{
"_id" : ObjectId("58738b9bf28356819ea81c3e"),
"emp_name" : "employee3",
"emp_id" : "techm1003",
"emp_age" : 27,
"emp_dob" : ISODate("1992-06-18T18:30:00Z"),
"emp_skils" : [
".net",
"C#",
"Spring",
"hibernate",
"MySql",
"MongoDB"
],
"emp_address" : [
{
"address1" : "Chennai",
"address2" : "Bangalore",
"address3" : "Pune"
}
]
}
{
"_id" : ObjectId("58738b9bf28356819ea81c3f"),
"emp_name" : "employee4",
"emp_id" : "techm1004",
"emp_age" : 28,
"emp_dob" : ISODate("1993-07-19T18:30:00Z"),
"emp_band" : "U3",
"emp_skils" : [
"Java",
"J2EE",
"Spring",
"hibernate",
"MySql",
"MongoDB"
],
"emp_address" : [
{
"address1" : "Chennai",
"address2" : "Bangalore",
"address3" : "Pune"
}
]
}
5 - To insert the document you can use db.post.save(document) also.
Case 1 - If you don't specify _id in the document then save () method will work same as
insert () method
Case 2 - If you specify _id then it will replace whole data of document containing _id as
specified in save () method.

8 - Query Document
Syntax
db.collection_name.find()
(Or)
db.collection_name.find().pretty();
Examples
Without pretty() method
With pretty() method

9 – Update document
Syntax – First document only updating
db.collection_name.update({SELECTION_CRITERIA},{ UPDATED_DATA})
Examples
>db.techmcolls5.update({emp_age:26},{$set:{emp_age:90}});
Syntax – With multi document updating
db.collection_name.update({SELECTION_CRITERIA},{ UPDATED_DATA},
{multi:true})
Examples
db.techmcolls5.update({emp_age:26},{$set:{emp_age:90}},{multi:true});
10 – Delete Document

Syntax
db.collection_name({SELECTION_CRITERIA},limit)
Example

>db.techmcolls5.remove({emp_age:90},1);

Syntax – to remove all document by given criteria


db.collection_name({SELECTION_CRITERIA})
Examples
db.techmcolls5.remove({emp_age:90});

You might also like