Explain the mongo DB working with node.js
Explain the mongo DB working with node.js
js
MongoDB is a NoSQL database that stores data in JSON-like format. In Node.js, MongoDB
can be connected and managed through the mongodb package or Mongoose ODM, which
allows a simple interface for CRUD (Create, Read, Update, Delete) operations.
2. Setup
Before running the code, ensure you have:
1. MongoDB installed and running.
2. Node.js and npm installed.
3. Required libraries installed by running:
npm install mongodb
// 1. Insert a Document
const student = { name: "John Doe", age: 21, department: "Computer Science" };
const result = await collection.insertOne(student);
console.log("Inserted document ID:", result.insertedId);
// 2. Find a Document
const query = { name: "John Doe" };
const foundStudent = await collection.findOne(query);
console.log("Found Student:", foundStudent);
// 3. Update a Document
const update = { $set: { age: 22 } };
const updateResult = await collection.updateOne(query, update);
console.log("Updated document count:", updateResult.modifiedCount);
// 4. Delete a Document
const deleteResult = await collection.deleteOne(query);
console.log("Deleted document count:", deleteResult.deletedCount);
} finally {
// Close the connection
await client.close();
console.log("Disconnected from MongoDB");
}
}
// Call the main function
main().catch(console.error);
Explanation of Code:
1. Connecting to MongoDB: The MongoClient connects to MongoDB using a
connection URI. Here, we connect to localhost on the default port (27017).
2. Database and Collection: We specify a testDB database and a students collection.
3. Insert Operation:
o We insert a document (student object) into the students collection.
o The result gives us the ID of the inserted document.
4. Find Operation:
o We search for a document where name is "John Doe".
o The result returns the document that matches the query.
5. Update Operation:
o We update the age field for the document found in the previous query.
o The $set operator modifies the age field to 22.
o The updateOne function returns a count of modified documents.
6. Delete Operation:
o We delete the document matching the query { name: "John Doe" }.
o The deleteOne function confirms the number of documents deleted.
7. Closing the Connection:
o The client.close() method disconnects from MongoDB after all operations are
complete.
4. Expected Output
If you run the script, you should see output like:
Connected to MongoDB
Inserted document ID: 64d2c0ed5f6abc0e7a6a8d29
Found Student: { _id: ObjectId("64d2c0ed5f6abc0e7a6a8d29"), name: 'John Doe', age: 21,
department: 'Computer Science' }
Updated document count: 1
Deleted document count: 1
Disconnected from MongoDB
5. Explanation of MongoDB Working with Node.js
In this example, MongoDB and Node.js work together as follows:
1. Node.js initiates a connection to MongoDB using the URI.
2. The connection allows Node.js to interact with MongoDB for database operations.
3. CRUD operations (Insert, Find, Update, Delete) demonstrate how data is managed
within MongoDB using Node.js.
4. Asynchronous Handling: The code uses async/await to handle asynchronous
operations, allowing smooth communication with the database.