What are CRUD operations in MongoDB?
Robert Cooper
Robert Cooper Senior Engineer at Basedash
· January 31, 2025
Robert Cooper
Robert Cooper Senior Engineer at Basedash
· January 31, 2025
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.
Creating, or inserting, data is the act of adding new documents to a collection in MongoDB.
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();
});
});
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();
});
Reading, or querying, data involves retrieving documents from a collection based on certain criteria.
To get a single document that matches a condition:
collection.findOne({name: 'John Doe'}, function(err, document) {
console.log(document);
});
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.
Updating involves modifying existing documents in a collection.
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.
To update multiple documents:
collection.updateMany({age: {$lt: 30}}, {$set: {status: 'young'}}, function(err, result) {
console.log('Updated documents');
});
Deleting is the removal of documents from a collection.
To remove a single document:
collection.deleteOne({name: 'John Doe'}, function(err, result) {
console.log('Deleted document');
});
To remove multiple documents:
collection.deleteMany({age: {$lt: 30}}, function(err, result) {
console.log('Deleted documents');
});
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.
Written by
Senior Engineer at Basedash
Robert Cooper is a senior engineer at Basedash who builds full-stack product systems across SQL data infrastructure, APIs, and frontend architecture. His work focuses on application performance, developer velocity, and reliable self-hosted workflows that make data operations easier for teams at scale.
Basedash lets you build charts, dashboards, and reports in seconds using all your data.