This content originally appeared on DEV Community and was authored by Ryoichi Homma
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:
- Documents and Collections
- Querying
- Data and Schema Modeling
- How to Use GridFS to Store Files
- 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' });
- Collections are containers for documents. You can view all collections in a database by using:
show collections;
- To list all available databases:
show dbs;
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
- Limiting the number of results:
db.users.find().limit(5); // return the first 5 documents
- Skipping results:
db.users.find().skip(10); // skip the first 10 documents
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();
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
- Retrieving files:
mongofiles -d mydb get myfile.txt
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' }]
});
-
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
- mongorestore: Restores the backup created by mongodump.
mongorestore --db mydb /backup/location
This content originally appeared on DEV Community and was authored by Ryoichi Homma
Ryoichi Homma | Sciencx (2024-10-20T23:53:53+00:00) MongoDB – Essential Things to Know. Retrieved from https://www.scien.cx/2024/10/20/mongodb-essential-things-to-know/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.