Mongo DB
Mongo DB
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
These 12 bytes
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
Big Data
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
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);