MongoDB is a popular NoSQL database known for its flexibility, scalability, and ease of use. Unlike traditional relational databases, MongoDB stores data in a schema-less format, using documents in a JSON-like structure called BSON (Binary JSON). This allows for rapid development and iteration, making it an ideal choice for modern applications that require agility and scalability.
In this tutorial, we will explore how to create a database in MongoDB.
Step 1 – Verify the MongoDB Installation
Before starting, it is essential to check whether MongoDB is installed on your system. You can check it using the following command.
mongo --version
You should see the information for the MongoDB version.
MongoDB shell version v4.4.29
Build Info: {
"version": "4.4.29",
"gitVersion": "f4dda329a99811c707eb06d05ad023599f9be263",
"openSSLVersion": "OpenSSL 1.1.0g 2 Nov 2017",
"modules": [],
"allocator": "tcmalloc",
"environment": {
"distmod": "ubuntu2004",
"distarch": "x86_64",
"target_arch": "x86_64"
}
}
Note: If you get an error, it means MongoDB isn’t installed or isn’t in your system’s PATH.
Step 2 – Connecting to MongoDB
After verifying the MongoDB installation, you can connect using the Mongo shell. Open your terminal or command prompt and type:
mongo
This command connects you to the MongoDB server and opens an interactive Mongo shell. You should see a prompt that looks like this:
>
Step 3 – Creating a Database
In MongoDB, you don’t need to create a database explicitly. Instead, you can switch to a non-existent database, and MongoDB will create it for you when you insert the first document. Let’s create a database named mydatabase.
use mydatabase
Output.
switched to db mydatabase
Initially, the database will not be visible in the list of databases because it doesn’t contain any data. To see the list of databases, use the command:
show dbs
Output:
admin 0.000GB
config 0.000GB
local 0.000GB
Let’s insert a sample document into a collection called mycollection within the mydatabase:
db.mycollection.insertOne({ name: "John Doe", age: 30, profession: "Developer" })
Output:
{
"acknowledged" : true,
"insertedId" : ObjectId("60c5dbd4f5d1dc8f459f2a27")
}
Now, if you run the show dbs command again, you’ll see mydatabase listed:
show dbs
Output:
admin 0.000GB
config 0.000GB
local 0.000GB
mydatabase 0.000GB
Step 4 – Basic Database Operations
You can create collections within a database to store your data. To create a new collection, use the createCollection method:
db.createCollection("mynewcollection")
Output:
{ "ok" : 1 }
To see all the collections in your current database, use the show collections command:
show collections
Output:
mycollection
mynewcollection
You can insert documents into a collection using the insertOne or insertMany methods:
db.mynewcollection.insertOne({ title: "MongoDB Guide", author: "Jane Doe" })
Output:
{
"acknowledged" : true,
"insertedId" : ObjectId("60c5dbf2f5d1dc8f459f2a28")
}
To retrieve documents from a collection, use the find method:
db.mynewcollection.find()
Output:
{ "_id" : ObjectId("60c5dbf2f5d1dc8f459f2a28"), "title" : "MongoDB Guide", "author" : "Jane Doe" }
To update documents, use the updateOne or updateMany methods. Let’s update the document we just inserted:
db.mynewcollection.updateOne({ title: "MongoDB Guide" }, { $set: { author: "John Smith" } })
Output:
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
To delete documents, use the deleteOne or deleteMany methods. Let’s delete the document we just updated:
db.mynewcollection.deleteOne({ title: "MongoDB Guide" })
Output.
{ "acknowledged" : true, "deletedCount" : 1 }
Conclusion
In this article, we have covered the essential steps to create and manage a database in MongoDB. We started with connecting to the MongoDB server, and created a new database by simply switching to it. We then explored basic database operations such as creating collections, inserting, querying, updating, and deleting documents. You can now easily create a MongoDB database on dedicated server hosting from Atlantic.Net!