Goglides Dev 🌱

Cover image for MongoDB Collections: What They Are and How to Work with Them
Balkrishna Pandey
Balkrishna Pandey

Posted on

MongoDB Collections: What They Are and How to Work with Them

Introduction

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.

What is a Collection?

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).

Collections vs. Tables in SQL

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.

Creating a Collection

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:

> db.createCollection("users")
Enter fullscreen mode Exit fullscreen mode

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).

Showing Collections

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
Enter fullscreen mode Exit fullscreen mode

This will print the names of all collections in the current database. You can also use the "getCollectionNames" method of the "db" object to get a JavaScript array of collection names.

Renaming a Collection

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:

> db.users.renameCollection("customers")
Enter fullscreen mode Exit fullscreen mode

This will rename the "users" collection to "customers" and return a "CommandResult" object.

Deleting a Collection

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:

> db.customers.drop()
Enter fullscreen mode Exit fullscreen mode

This will remove the "customers" collection and all its documents from the database.

Conclusion

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

Top comments (0)