Before diving into creating tables, it’s essential to understand what a collection is in MongoDB. A collection is a group of MongoDB documents, similar to a table in a relational database. Documents within a collection can have different fields, but typically, they share a similar purpose or structure. Collections do not enforce a schema, meaning each document can contain different fields and data types.
In this tutorial, we’ll learn how to create a table in MongoDB.
Step 1 – Connecting to MongoDB
First, we need to connect to the MongoDB server. Open your terminal or command prompt and start the MongoDB shell by typing:
mongo
You should see an output indicating that you are connected to the MongoDB server.
>
Step 2 – Creating a Database
Before creating a collection, we need a database. In MongoDB, databases are created dynamically. You can switch to a new or existing database using the use command. For this tutorial, we’ll create a new database called tutorialDB.
use tutorialDB
The output will be:
switched to db tutorialDB
Step 3 – Creating a Collection
In MongoDB, collections are created implicitly when you insert data into them. However, you can also create a collection explicitly using the createCollection command.
Let’s create a collection named users by inserting a document into it:
db.users.insertOne({
name: "John Doe",
email: "[email protected]",
age: 29
})
This command creates a collection named users and inserts a document with the specified fields.
{
"acknowledged" : true,
"insertedId" : ObjectId("669514de0bc92b8bfdbe553d")
}
Alternatively, you can explicitly create a collection using the createCollection command:
db.createCollection("products")
This command creates an empty collection named products.
{ "ok" : 1 }
Step 4 – Inserting Documents into a Collection
Now that we have our collections, let’s insert more documents into them.
To insert multiple documents into the users collection, use the insertMany command:
db.users.insertMany([
{ name: "Alice Smith", email: "[email protected]", age: 24 },
{ name: "Bob Johnson", email: "[email protected]", age: 35 },
{ name: "Charlie Brown", email: "[email protected]", age: 28 }
])
Output.
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("669515120bc92b8bfdbe553e"),
ObjectId("669515120bc92b8bfdbe553f"),
ObjectId("669515120bc92b8bfdbe5540")
]
}
To insert a single document into the products collection, use the insertOne command:
db.products.insertOne({
productName: "Laptop",
brand: "BrandX",
price: 799
})
The output will be:
{
"acknowledged" : true,
"insertedId" : ObjectId("669515310bc92b8bfdbe5541")
}
Step 5 – Querying Collections
Once we have data in our collections, we can query it. Here are a few basic queries to get you started.
To find all documents in a collection, use the find command:
db.users.find().pretty()
Output:
{
"_id" : ObjectId("669514de0bc92b8bfdbe553d"),
"name" : "John Doe",
"email" : "[email protected]",
"age" : 29
}
{
"_id" : ObjectId("669515120bc92b8bfdbe553e"),
"name" : "Alice Smith",
"email" : "[email protected]",
"age" : 24
}
{
"_id" : ObjectId("669515120bc92b8bfdbe553f"),
"name" : "Bob Johnson",
"email" : "[email protected]",
"age" : 35
}
{
"_id" : ObjectId("669515120bc92b8bfdbe5540"),
"name" : "Charlie Brown",
"email" : "[email protected]",
"age" : 28
}
The pretty method formats the output for readability.
To find specific documents, pass a query document to the find command. For example, to find users who are 29 years old:
db.users.find({ age: 29 }).pretty()
The output will be:
{
"_id" : ObjectId("669514de0bc92b8bfdbe553d"),
"name" : "John Doe",
"email" : "[email protected]",
"age" : 29
}
Step 6 – Updating Documents
Updating documents in MongoDB is straightforward. Let’s see how to update a document’s field.
To update a single document, use the updateOne command. For example, to update John Doe’s age:
db.users.updateOne(
{ name: "John Doe" },
{ $set: { age: 30 } }
)
To update multiple documents, use the updateMany command. For example, to set the verified field to true for all users:
db.users.updateMany(
{},
{ $set: { verified: true } }
)
Step 7 – Deleting Documents
You can also delete documents from a collection.
To delete a single document, use the deleteOne command. For example, to delete Bob Johnson’s document:
db.users.deleteOne({ name: "Bob Johnson" })
To delete multiple documents, use the deleteMany command. For example, to delete all users who are younger than 30:
db.users.deleteMany({ age: { $lt: 30 } })
Conclusion
In this article, we covered the basics of creating and managing collections (tables) in MongoDB. We learned how to connect to MongoDB, create a database, create collections implicitly and explicitly, insert documents, query data, update documents, and delete documents. Try to create a MongoDB table on dedicated server hosting from Atlantic.Net!