- What is a Collection?
- Collections vs. Tables in SQL
- Creating a Collection
- Showing Collections
- Renaming a Collection
- Deleting a Collection
In the previous post, we covered the basics of MongoDB, including installation and usage of the mongod and mongosh tools. Now, let's delve into collections, the main containers for MongoDB documents, and learn how to create, show, rename, and delete them. We'll also compare collections with tables in SQL databases.
Collections are MongoDB's primary unit of data organization and provide a flexible and scalable way to store and access data. Collections are dynamic and schema-less, meaning that they can grow or shrink as needed, and individual documents can have different fields or structures. In MongoDB, a collection is a group of documents that share a similar structure and are held together in the same namespace (i.e., database + collection).
If you're familiar with SQL databases, you may wonder how collections differ from tables. While both collections and tables organize data into rows and columns, they have some notable differences:
- Collections store documents, which are JSON-like objects, whereas tables store records, which are structured according to a fixed schema.
- Collections can have a variable number of fields and types for each document, while tables have a fixed number of columns and data types.
- Collections are typically denormalized, meaning that related data may be nested within a single document, while tables rely on joins and foreign keys to link data across multiple tables.
To create a new collection in MongoDB, you can use the "createCollection" method, which takes a collection name and an optional set of options as parameters. For example, to create a collection called "users" in the "mydb" database, you can run the following command in the mongosh shell:
This will create an empty "users" collection with default options. You can also specify options such as "capped" (for a fixed-size collection), "autoIndexId" (for automatic creation of an "_id" index), and "validator" (for enforcing a schema or document validation rules).
To list all the collections in a database, you can use the "show collections" command in the mongosh shell. For example, to list all the collections in the "mydb" database, you can run:
> show collections
To rename a collection in MongoDB, you can use the "renameCollection" method, which takes the current collection name and the new collection name as parameters. For example, to rename the "users" collection to "customers" in the "mydb" database, you can run:
This will rename the "users" collection to "customers" and return a "CommandResult" object.
To delete a collection in MongoDB, you can use the "drop" method, which takes the collection name as a parameter. For example, to delete the "customers" collection in the "mydb" database, you can run:
This will remove the "customers" collection and all its documents from the database.
Collections are a fundamental concept in MongoDB and provide a flexible and scalable way to organize and access data. By understanding how to create, show, rename, and delete collections, you can take full advantage of MongoDB's dynamic and schema-less data