Hello there! Welcome to the next lesson of our "Introduction to MongoDB" course. In the previous lesson, we covered the basics of MongoDB, including its differences from SQL databases and its common use-cases.
In this lesson, we'll focus on creating, managing, and deleting databases and collections in MongoDB. We'll start by understanding the MongoDB architecture, including the Server and Client components. Next, we'll cover key terms like documents, collections, and databases. Finally, we'll learn how to create and manage databases and collections using MongoDB commands. This will set you up for practical exercises to solidify your understanding.
Before we dive into creating databases and collections, let's understand the architecture of MongoDB. MongoDB consists of two main components: the MongoDB Server and the MongoDB Client.
-
MongoDB Server: This is the core database server that manages the storage, retrieval, and updating of data. It handles database operations and provides a robust system for data management.
-
MongoDB Client: This is the interface through which users interact with the MongoDB Server. Clients can be command-line tools, APIs, or GUI tools like MongoDB Compass. Additionally, MongoDB provides drivers for various programming languages, such as Node.js, Python, Java, and more, allowing developers to interact with the database using their preferred language.
There are a few possible setups:
-
Local Setup (Client and Server on the Same Machine): For learning purposes, you can set up both the MongoDB Server and Client on the same machine. This setup is convenient for individual practice and development.
-
Production Setup (Client and Server on Separate Machines): In a production environment, the MongoDB Server is typically hosted on a separate machine, often in a Cloud Environment, for better performance and scalability. A popular option for cloud-based MongoDB is MongoDB Atlas, a fully-managed database-as-a-service that handles the complexities of deploying, managing, and scaling MongoDB.
-
Documents: In MongoDB, the basic unit of data is the "document." A document is a set of key-value pairs, similar to JSON objects. Each document can have a different structure, meaning one document can have fields that another document in the same collection does not have.
-
Collections: Collections in MongoDB are analogous to tables in relational databases. They are used to group documents together. Unlike tables in SQL databases, collections in MongoDB do not enforce a schema, allowing documents in a single collection to have different fields.
-
Databases: A database is a container for collections. Each database gets its own set of files on the file system. Databases in MongoDB are independent of each other.
Creating a new database in MongoDB is simple. MongoDB creates databases implicitly upon their first use. Use the following command to create and switch to a new database:
1use <databaseName>
For example, to create a database named "library_db":
JavaScript1use library_db
Naming Conventions: Database names should adhere to the following rules:
- Must be fewer than 64 characters.
- Cannot include spaces or any of the following characters:
/
,\
,.
,"
,$
, null character. - Should be lowercase to avoid confusion, although MongoDB is case-sensitive.
Examples of good database names: customer_data_db
, inventory_db
, salesRecordsDb
, userAccountsDb
To view all databases in your current MongoDB setup, use the command:
1show dbs
However, note that the database will not be listed until it contains some data.
To delete a database, ensure you are in the database you wish to remove and use the following command:
JavaScript1use library_db 2 3db.dropDatabase()
Be cautious, as this operation is irreversible and will remove all collections and data within the specified database.
Creating a collection can be done using the db.createCollection()
method. Let's create a collection named "books" in the "library" database:
JavaScript1use library_db 2 3db.createCollection("books")
Naming Conventions: Collection names should follow these guidelines:
- Cannot be an empty string.
- Must not contain the character null character, as it signifies the end of the collection name.
- Should not start with the system-reserved namespaces
system.
or use the$
character.
Additionally, collection names are case-sensitive, so users
and Users
would be considered different collections.
Examples of good collection names: users
, orders
, product_catalog
, customerData
To view all collections within the current database, use the command:
JavaScript1use library_db 2 3show collections
To delete a collection, use the following command:
1db.<collectionName>.drop()
For example, to delete the "books" collection:
JavaScript1use library_db 2 3db.books.drop()
Be cautious, as this operation is irreversible and will remove all documents within the specified collection.
We explored the essentials of creating, managing, and deleting databases and collections in MongoDB. Key MongoDB terms like documents, collections, and databases were discussed, along with how to manage them using MongoDB commands. With this foundation, you're now prepared to proceed with practical exercises that provide hands-on experience in managing databases and collections in MongoDB. Keep practicing and enjoy your journey!