What are CRUD operations in MongoDB?
MongoDB, a popular NoSQL database, operates using documents (usually in a JSON-like format known as BSON) and collections instead of the typical rows and tables in relational databases. One of the fundamental aspects of using any database is the ability to Create, Read, Update, and Delete data – commonly referred to as CRUD operations. In MongoDB, these operations can be executed with a variety of methods.
In this guide, we will delve into each CRUD operation in MongoDB, and you'll learn how to effectively use them.
C - Create
Creating, or inserting, data is the act of adding new documents to a collection in MongoDB.
Insert One Document
To add a single document, you can use the insertOne
method.
const MongoClient = require('mongodb').MongoClient; const url = 'mongodb://localhost:27017'; const dbName = 'mydatabase'; MongoClient.connect(url, function(err, client) { const db = client.db(dbName); const collection = db.collection('documents'); collection.insertOne({ name: 'John Doe', age: 30 }, function(err, result) { console.log('Inserted document'); client.close(); }); });
Insert Multiple Documents
If you have multiple documents, the insertMany
method is more suitable.
const documents = [{ name: 'Alice', age: 28 }, { name: 'Bob', age: 25 }]; collection.insertMany(documents, function(err, result) { console.log('Inserted documents'); client.close(); });
R - Read
Reading, or querying, data involves retrieving documents from a collection based on certain criteria.
Find One Document
To get a single document that matches a condition:
collection.findOne({name: 'John Doe'}, function(err, document) { console.log(document); });
Find Multiple Documents
To retrieve multiple documents:
collection.find({age: {$gt: 25}}).toArray(function(err, documents) { console.log(documents); });
In this example, we're using the $gt
operator to find all documents where the age is greater than 25.
U - Update
Updating involves modifying existing documents in a collection.
Update One Document
To update a single document:
collection.updateOne({name: 'John Doe'}, {$set: {age: 31}}, function(err, result) { console.log('Updated document'); });
Here, we're using the $set
operator to modify the age of John Doe.
Update Multiple Documents
To update multiple documents:
collection.updateMany({age: {$lt: 30}}, {$set: {status: 'young'}}, function(err, result) { console.log('Updated documents'); });
D - Delete
Deleting is the removal of documents from a collection.
Delete One Document
To remove a single document:
collection.deleteOne({name: 'John Doe'}, function(err, result) { console.log('Deleted document'); });
Delete Multiple Documents
To remove multiple documents:
collection.deleteMany({age: {$lt: 30}}, function(err, result) { console.log('Deleted documents'); });
Conclusion
CRUD operations form the backbone of many applications, allowing for effective data management. With MongoDB's versatile methods and easy-to-understand syntax, performing CRUD operations is both straightforward and efficient.
Remember, while these examples demonstrate the basic usage of CRUD operations, MongoDB offers a wide range of querying and updating capabilities, including complex aggregations and atomic operations. Always refer to the official MongoDB documentation for a deep dive into advanced features and best practices.
Invite only
We're building the next generation of data visualization.
How to Center a Table in HTML with CSS
Jeremy Sarchet
Adjusting HTML Table Column Width for Better Design
Robert Cooper
How to Link Multiple CSS Stylesheets in HTML
Robert Cooper
Mastering HTML Table Inline Styling: A Guide
Max Musing
HTML Multiple Style Attributes: A Quick Guide
Max Musing
How to Set HTML Table Width for Responsive Design
Max Musing