DEV Community

Cover image for MongoDB - Essential Things to Know
Ryoichi Homma
Ryoichi Homma

Posted on

MongoDB - Essential Things to Know

MongoDB is a non-relational database (NoSQL), known for its flexibility and scalability. This article will focus on understanding key concepts like documents, collections, querying, schema modeling, and even more advanced features such as GridFS and server administration.

Concept Highlights:

  1. Documents and Collections
  2. Querying
  3. Data and Schema Modeling
  4. How to Use GridFS to Store Files
  5. Server Administration

1. Documents and Collections

In MongoDB, documents are individual records stored in collections, which are analogous to tables in a relational database. Each document is a JSON-like structure.

  • Querying documents with .find(): the .find() method allows you to query the documents within a collection.
db.users.find({ name: 'Freddie Freeman' });
Enter fullscreen mode Exit fullscreen mode
  • Collections are containers for documents. You can view all collections in a database by using:
show collections;
Enter fullscreen mode Exit fullscreen mode
  • To list all available databases:
show dbs;
Enter fullscreen mode Exit fullscreen mode

2. Querying

MongoDB offers a powerful querying system that allows you to refine your data using sort, limit, and skip.

  • Sorting your results:
db.users.find().sort({ age: 13 }); // ascending order by age
Enter fullscreen mode Exit fullscreen mode
  • Limiting the number of results:
db.users.find().limit(5);  // return the first 5 documents
Enter fullscreen mode Exit fullscreen mode
  • Skipping results:
db.users.find().skip(10); // skip the first 10 documents
Enter fullscreen mode Exit fullscreen mode

MongoDB Shell & mongosh:

  • mongosh is the new MongoDB shell, which provides an enhanced experience for querying and database interaction.

  • To format your query results nicely:

db.users.find().pretty();
Enter fullscreen mode Exit fullscreen mode

3. Data and Schema Modeling

One of the advantages of MongoDB is its flexibility with schema design, but structuring your data efficiently is crucial for performance and scalability.

  • Documents in MongoDB do not require a predefined schema, unlike relational databases.

  • Schema design revolves around the nature of your queries:

    • Embedded documents: Useful for nested data that is retrieved together.
    • References: Use when you need to maintain relationships between documents (similar to foreign keys).

Modeling data correctly will have a big impact on performance when working with large datasets.

4. How to Use GridFS to Store Files

GridFS is a powerful feature of MongoDB that enables you to store and retrieve large files such as images, videos, and backups (exceeding the BSON-document size limit of 16MB) by splitting them into chunks.

  • Storing files in GridFS:
mongofiles -d mydb put myfile.txt
Enter fullscreen mode Exit fullscreen mode
  • Retrieving files:
mongofiles -d mydb get myfile.txt
Enter fullscreen mode Exit fullscreen mode

5. Server Administration

  • MongoDB Config File:
    MongoDB can configured through its config file, usually located at /etc/mongod.conf. This file contains all essential settings, like the port number, storage path, and authentication settings.

  • Replication:
    Replication allows you to create copies of your MongoDB data across multiple servers, ensuring high availability and redundancy.

    • You can set up a replica set, which is a group of MongoDB instances that maintain the same data set.
  • Sharding:

    Sharding is a method for distributing data across multiple servers. MongoDB uses sharding to handle very large datasets by splitting them into smaller, more manageable pieces known as shards.

  • Authentication and Authorization:
    MongoDB supports built-in authentication and authorization mechanisms. You can control access to your databases by creating users with specific roles, granting them permissions as needed.

    • To create a new user with read-only permissions:
db.createUser({
  user: 'readonly',
  pwd: 'password',
  roles: [{ role: 'read', db: 'mydb' }]
});
Enter fullscreen mode Exit fullscreen mode
  • Backups:
    Backing up your MongoDB data is crucial for data integrity. There are several ways to backup MongoDB data:

    • mongodump: Exports the data into BSON format.
mongodump --db mydb --out /backup/location
Enter fullscreen mode Exit fullscreen mode
  • mongorestore: Restores the backup created by mongodump.
mongorestore --db mydb /backup/location
Enter fullscreen mode Exit fullscreen mode

Top comments (0)