How to Perform MongoDB CRUD Operations?

How to Perform MongoDB CRUD Operations?

How to Perform MongoDB CRUD Operations?

MongoDB is a popular and scalable NoSQL database solution for modern applications. CRUD operations (Create, Read, Update, Delete) are a basic necessity for saving, updating, querying, and deleting data in your applications. In this article, essential information, code examples, and practical tips about MongoDB CRUD operations will be provided.

Basics of CRUD Operations in MongoDB

CRUD operations refer to the most fundamental command set that everyone dealing with database management should know. Since MongoDB is a document-based database, operations are carried out on JSON-like BSON documents. Below are detailed MongoDB CRUD operation examples in a Node.js environment.

1. Create: Adding Data


const { MongoClient } = require('mongodb');

async function createDocument() {
  const client = new MongoClient('mongodb://localhost:27017');
  await client.connect();
  const db = client.db('blog');
  const collection = db.collection('posts');
  const result = await collection.insertOne({
    title: 'MongoDB CRUD Operations',
    content: 'Performing operations in MongoDB with CRUD is very easy!',
    date: new Date()
  });
  console.log('Inserted document ID:', result.insertedId);
  await client.close();
}
createDocument();

2. Read: Reading Data


async function readDocuments() {
  const client = new MongoClient('mongodb://localhost:27017');
  await client.connect();
  const db = client.db('blog');
  const collection = db.collection('posts');
  const docs = await collection.find({ title: 'MongoDB CRUD Operations' }).toArray();
  console.log(docs);
  await client.close();
}
readDocuments();

3. Update: Updating Data


async function updateDocument() {
  const client = new MongoClient('mongodb://localhost:27017');
  await client.connect();
  const db = client.db('blog');
  const collection = db.collection('posts');
  const result = await collection.updateOne(
    { title: 'MongoDB CRUD Operations' },
    { $set: { content: 'Data successfully updated with MongoDB!' } }
  );
  console.log('Updated document:', result.modifiedCount);
  await client.close();
}
updateDocument();

4. Delete: Deleting Data


async function deleteDocument() {
  const client = new MongoClient('mongodb://localhost:27017');
  await client.connect();
  const db = client.db('blog');
  const collection = db.collection('posts');
  const result = await collection.deleteOne({ title: 'MongoDB CRUD Operations' });
  console.log('Deleted document:', result.deletedCount);
  await client.close();
}
deleteDocument();

Conclusion: The Importance of MongoDB CRUD Operations

With MongoDB CRUD operations, you can easily perform all basic operations on your database. To ensure efficient and fast data management in modern applications, it is important to correctly implement CRUD operations. With these examples, a good start has been made for those new to MongoDB or those who want to refresh their knowledge. You can create flexible, secure, and scalable data solutions in your projects with MongoDB CRUD operations.