⬅ Previous Topic
Introduction to MongoDB Atlas (Cloud)Next Topic ⮕
JSON vs BSON in MongoDB⬅ Previous Topic
Introduction to MongoDB Atlas (Cloud)Next Topic ⮕
JSON vs BSON in MongoDBMongoDB organizes data in a structure that is different from traditional relational databases. To use MongoDB effectively, it's essential to understand its three core building blocks: databases, collections, and documents.
A database in MongoDB is a container that holds collections, which in turn contain documents. Each database has its own set of files on the disk and is isolated from other databases.
A collection is a group of MongoDB documents, similar to a table in relational databases. However, collections are schema-less, meaning documents inside them can have different fields.
A document is a single record in MongoDB, stored in BSON (Binary JSON) format. It is similar to a row in an SQL table, but far more flexible — allowing nested objects and arrays.
Let’s create a new database and collection using the MongoDB Shell.
mongo
use school
db.createCollection("students")
{ "ok" : 1 }
Explanation: Here, we switch to a new database called school
. If it doesn't exist, MongoDB will create it once we insert data. Then we create a collection called students
.
Now let’s insert a student document into the students
collection.
db.students.insertOne({
name: "Rahul Sharma",
age: 20,
courses: ["Math", "Physics"],
address: {
city: "Delhi",
pincode: 110001
}
})
{ acknowledged: true, insertedId: ObjectId("...") }
Explanation: This document represents one student. It includes a name, age, an array of courses, and a nested object for address. This flexible schema allows us to store rich, structured data in one place without complex joins.
Q: Can the next student we insert have a completely different structure?
A: Yes! MongoDB collections do not require all documents to follow the same schema.
db.students.insertOne({
name: "Aditi Mehta",
year: "Final",
hobbies: ["painting", "reading"]
})
{ acknowledged: true, insertedId: ObjectId("...") }
Explanation: This document doesn't have age
, courses
, or address
fields. That’s perfectly fine in MongoDB.
You can view the inserted documents using find()
:
db.students.find().pretty()
{ "_id": ObjectId("..."), "name": "Rahul Sharma", "age": 20, "courses": ["Math", "Physics"], "address": { "city": "Delhi", "pincode": 110001 } } { "_id": ObjectId("..."), "name": "Aditi Mehta", "year": "Final", "hobbies": ["painting", "reading"] }
Q: Do we need to define the fields before inserting documents?
A: No. MongoDB allows dynamic documents, so you can insert any structure at any time. This is great for evolving data models.
MongoDB's structure is highly flexible and intuitive once you grasp these three concepts. In the next topic, we’ll explore MongoDB’s supported data types in more detail.
⬅ Previous Topic
Introduction to MongoDB Atlas (Cloud)Next Topic ⮕
JSON vs BSON in MongoDBYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.