Lecture Note Of Day 3 - MongoDB Basic Concepts

Rashmi Mishra
0

 

PREVIOUS                                                                               NEXT

Lecture Note Of Day 3 

MongoDB Basic Concepts

Objective:

By the end of this session, students will understand the fundamental concepts of MongoDB, specifically focusing on databases, collections, and documents.


Introduction to MongoDB

MongoDB is a NoSQL database that stores data in a flexible, JSON-like format called BSON (Binary JSON). It is designed for scalability, high availability, and performance. Unlike relational databases that store data in tables and rows, MongoDB uses collections and documents to store data.


Key Concepts

1. Database

A database in MongoDB is a container for collections. It is similar to a database in relational database systems (RDBMS) but has a flexible structure.

  • A MongoDB instance can have multiple databases.
  • Each database can have multiple collections.
  • A database in MongoDB does not require a predefined schema.

Commands for Working with Databases:

  • Show Databases: To see the databases available in the MongoDB instance:

show databases

  • Create Database: To create a new database (the database is created when a collection is inserted):

use <database_name>

2. Collection

A collection in MongoDB is equivalent to a table in relational databases. It is a group of documents stored together, and collections are schema-less, meaning the structure of documents within a collection can vary.

  • Collections are created when you insert the first document into a database.
  • A collection can store any type of data and can be modified as needed.

Commands for Working with Collections:

  • Show Collections: To see the collections in a database:

show collections

  • Create Collection: To explicitly create a collection (although MongoDB will automatically create a collection when you insert data):

db.createCollection("<collection_name>")

3. Document

A document is the basic unit of data in MongoDB. It is a record in a collection, represented in BSON format. Each document is a set of key-value pairs, similar to JSON objects.

  • Each document can have different fields, and fields can contain various types of data (strings, numbers, arrays, embedded documents).
  • Documents have a unique _id field, which is automatically created by MongoDB. This field serves as the primary key for each document.

Example of a Document:

{

  "_id": ObjectId("607c35c2f5f1a5e6c55e1b22"),

  "name": "John Doe",

  "age": 30,

  "email": "john.doe@example.com",

  "address": {

    "street": "123 Main St",

    "city": "Springfield"

  }

}

Commands for Working with Documents:

  • Insert Document: To insert a document into a collection:

db.<collection_name>.insertOne({ "name": "John Doe", "age": 30 })

  • Find Document: To query and retrieve documents from a collection:

db.<collection_name>.find({ "name": "John Doe" })


Summary of Differences

Feature

Database

Collection

Document

Definition

A container for collections.

A group of documents.

A single record in BSON format.

Example

test

users

{ "_id": 1, "name": "John" }

Structure

No predefined schema.

Schema-less, can store any data.

Can contain various data types.

Command

use <database_name>

db.createCollection(<name>)

db.collection.insertOne(<doc>)


Practical Exercise

1.   Create a new database called school.

2.   Create a collection named students within the school database.

3.   Insert a document into the students collection with the following data:

{ "name": "Alice", "age": 25, "grade": "A" }

4.   Query the students collection to retrieve the inserted document.


Conclusion

MongoDB is a NoSQL database that uses databases, collections, and documents as its core concepts. Understanding these fundamental building blocks is essential for effectively working with MongoDB. These structures provide flexibility in terms of schema design, allowing for dynamic and scalable data storage solutions.


Homework Assignment:

1.   Define the differences between a relational database table and a MongoDB collection.

2.   Write a document for a products collection containing name, price, and category fields.

3.   Query a MongoDB database to find all documents where the price is greater than 100.



Tags

Post a Comment

0Comments

Post a Comment (0)

About Me