Welcome to the world of NoSQL databases! Today, we're diving into MongoDB – a popular, user-friendly NoSQL database that uses JSON-like documents and flexible data structures. Let's get started!
MongoDB is a versatile, open-source database that stores data in a format called BSON (Binary JSON) as collections of documents. It's great for handling large volumes of diverse, unstructured data and is widely used in web development, mobile applications, and big data projects.
In MongoDB, a database is a container for collections, indexes, and users. It's the highest level in the MongoDB hierarchy. You can create multiple databases to separate and manage different types of data more effectively.
A collection is a group of documents with a common schema. Each database can have multiple collections, and collections do not need to have the same structure or number of fields. This allows for flexible, schema-less data modeling.
A document is the basic unit of data storage in MongoDB. It's a collection of key-value pairs, similar to JSON objects, and can hold various data types, such as strings, numbers, arrays, and nested objects.
Now, let's get our hands dirty by creating a simple MongoDB database and collection using the MongoDB shell.
First, ensure that MongoDB is installed and running on your system.
Open your terminal (command prompt on Windows) and type mongo to connect to the MongoDB shell.
To create a new database, use the use command followed by the desired database name. For example:
use myDatabase
This creates a new database called myDatabase.
Now, let's create a collection called users. Use the following command:
db.createCollection("users")
This creates a new collection called users within the myDatabase database.
Next, let's add some documents to our users collection. Use the db.users.insertOne() method to insert a single document or db.users.insertMany() to add multiple documents.
Here's an example of inserting a single user document:
db.users.insertOne({
name: "John Doe",
email: "john.doe@example.com",
age: 30,
hobbies: ["reading", "swimming", "coding"]
})
And here's how to insert multiple users using insertMany():
db.users.insertMany([
{
name: "Jane Smith",
email: "jane.smith@example.com",
age: 28,
hobbies: ["painting", "hiking", "cooking"]
},
{
name: "Mike Johnson",
email: "mike.johnson@example.com",
age: 35,
hobbies: ["gaming", "photography", "traveling"]
}
])
Now that we have some data, let's learn how to query our documents. Use the find() method to retrieve data from the collection. You can specify filters, projections, and sorting options to fine-tune your queries.
Here's an example of querying users with the name "John Doe" or "Jane Smith":
db.users.find({ name: { $or: [ "John Doe", "Jane Smith" ] } })
You can also sort the results based on the age field:
db.users.find().sort({ age: 1 }) // for ascending order
db.users.find().sort({ age: -1 }) // for descending order
What is the basic unit of data storage in MongoDB?
Congratulations on completing this introductory tutorial on MongoDB databases, collections, and documents! In the next lesson, we'll dive deeper into querying, indexing, and data modeling techniques to help you master MongoDB and become a more effective data wrangler.
Stay tuned and happy coding! 💻🤓🚀