Big Data (Unit 3)
Big Data (Unit 3)
INTRODUCTION TO 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: 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: Collection is a group of MongoDB
documents. It is the equivalent of an RDBMS table.
A collection exists within a single database.
• Document
• 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 )
• {
• _id: ObjectId(7df78ad8902c)
• title: 'MongoDB Overview',
• description: 'MongoDB is no sql database',
• by: 'tutorials point',
• url: 'http://www.tutorialspoint.com',
• tags: ['mongodb', 'database', 'NoSQL'],
• likes: 100,
• comments: [
• {
• user:'user1',
• message: 'My first comment',
• dateCreated: new Date(2011,1,20,2,15),
• like: 0
• },
• {
• user:'user2',
• message: 'My second comments',
• dateCreated: new Date(2011,1,25,7,45),
like: 5
}
]
}
• Advantages of MongoDB over RDBMS
• 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.
• Structure of a single object is clear.
• No complex joins.
• Deep query-ability. MongoDB supports dynamic
queries on documents using a documentbased
query language that's nearly as powerful as SQL.
• Tuning.
• Ease of scale-out − MongoDB is easy to scale.
• Conversion/mapping of application objects to
database objects not needed.
• Uses internal memory for storing the
(windowed) working set, enabling faster access
of data.
• Why Use MongoDB?
• Document Oriented Storage − Data is stored in the form of
JSON style documents.
• Index on any attribute
• Replication and high availability
• Auto-Sharding
• Rich queries
• Fast in-place updates
• Professional support by MongoDB
• Where to Use MongoDB?
• Big Data
• Content Management and Delivery
• Mobile and Social Infrastructure
• User Data Management
• Data Hub
• MongoDB supports many datatypes. Some of them are −
• String − This is the most commonly used datatype to store the data. String in
MongoDB must be UTF-8 valid.
• Integer − This type is used to store a numerical value. Integer can be 32 bit or 64 bit
depending upon your server.
• Boolean − This type is used to store a boolean (true/ false) value.
• Double − This type is used to store floating point values.
• Min/ Max keys − This type is used to compare a value against the lowest and highest
BSON elements.
• Arrays − This type is used to store arrays or list or multiple values into one key.
• Timestamp − ctimestamp. This can be handy for recording when a document has been
modified or added.
• Object − This datatype is used for embedded documents.
• Null − This type is used to store a Null value.
• Symbol − This datatype is used identically to a string; however, it's generally reserved
for languages that use a specific symbol type.
• 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.
• Object ID − This datatype is used to store the document’ID
• Creation of collection :1⃣ Automatic Collection
Creation (Default Behavior):MongoDB
automatically creates a collection when you
insert a document into it.
EX:-db.users.insertOne({ name: "Alice", age: 25 });
2.Explicitly Creating a Collection:
• If you want to create a collection before inserting
documents, use the createCollection() method.
EX:-db.createCollection("employees");
• ✅ This creates an empty collection named
employees.
2. 2. Insert Methods (Adding Data)
Method Description
db.collection.insert([doc1,doc2,doc3])