Inserting Collections and Documents in MongoDB
Inserting Collections and Documents in MongoDB
In this tutorial, we will be diving into creating collections and inserting documents in MongoDB, building on the concepts we learned in the previous lecture. MongoDB uses three primary components to organize data: databases, collections, and documents. Let’s break each of these down and learn how to use them effectively.
Understanding Databases, Collections, and Documents
-
Database: A MongoDB database is equivalent to a project. It is a container for collections, which hold documents. In this tutorial, we are working with a database called
MongoSample
, which we created in the previous lecture. This database will hold all the collections we need for our application. -
Collection: A collection is the equivalent of a table in a traditional SQL database. However, unlike tables, collections do not require predefined schemas. Collections hold documents that are organized in a flexible, dynamic manner. In MongoDB, collections are created when documents are inserted into them.
-
Document: A document is a record in MongoDB that contains data in the form of key-value pairs. It is stored in collections and is similar to a row in an SQL table. Unlike traditional databases, each document can have a different structure, allowing for great flexibility.
In this tutorial, we will create our first collection and insert documents into it.
Step 1: Start MongoDB and Access the Mongo Shell
Before proceeding, ensure that your MongoDB server (using the mongod
command) is running. Open a separate terminal window and start the Mongo shell by typing mongo
.
Once in the Mongo shell, navigate to the MongoSample
database that we created earlier by typing:
use MongoSample
This command switches you to the MongoSample
database, allowing you to start working with it.
Step 2: Create a Collection
In MongoDB, collections do not require an explicit creation step. Instead, they are created dynamically the moment you insert your first document. To create a new collection called users
, simply reference it in your code while inserting a document.
Here’s an example:
db.users.insert({ name: "Henry", age: 26, email: "henry@gmail.com" })
In this command:
db
refers to the current database (MongoSample
in our case).users
is the name of the collection, similar to a table in SQL..insert()
is the method we use to add a new document into theusers
collection.
Since users
didn’t exist before, MongoDB automatically creates it and inserts the document.
Step 3: Inserting Multiple Documents
We can also insert multiple documents by running the .insert()
command again. Here is another example:
db.users.insert({ name: "Ben", age: 33, email: "ben@02geek.com" })
Unlike traditional databases where you have to define a table schema beforehand, MongoDB allows each document in a collection to have its own unique structure. Let’s insert another document, this time with additional attributes:
db.users.insert({ name: "Bell", age: 50, email: "bubbybell@gmail.com", children: ["Ben", "Sharon", "Laurie"] })
In this case, the document includes an additional field, children
, which contains an array of names. This demonstrates MongoDB's flexibility, as each document can have different attributes without a predefined schema.
Benefits of MongoDB’s Flexibility
Traditional databases require all records in a table to adhere to a strict schema. For example, fields like name
, email
, and age
must be predefined, and every record must conform to this format. This rigidity makes changing data structures cumbersome.
In MongoDB, documents are dynamic. You can add or remove fields at any time without worrying about schema constraints. This makes it easy to adapt to new requirements during the development process.
For instance, in our example, Bell’s document contains a children
array, while Henry's and Ben's documents do not. This kind of flexibility is incredibly useful in modern web development, where requirements can change rapidly.
Summary
In this tutorial, you learned about MongoDB databases, collections, and documents. We practiced creating a collection by inserting documents directly into it, illustrating MongoDB's flexibility compared to traditional SQL databases. Each document can be unique, making MongoDB a great choice for dynamic, rapidly changing applications.
In the next lecture, we’ll cover how to query our database to retrieve specific documents, enabling us to interact with the data we've inserted.
Note: This video was created in 2016. MongoDB has evolved since then, with updated features and methods. For example, newer versions of MongoDB support
insertOne()
andinsertMany()
for more explicit document insertion, and the aggregation framework has been enhanced for more advanced data processing. Please refer to the official MongoDB documentation for the latest updates and features.