In this tutorial, we are going to learn how to perform CRUD (Create, Retrieve, Update, Delete) operations in MongoDB using the MongoDB Node.js driver.
By the end of this tutorial, you will be able to:
- Connect to a MongoDB database
- Insert documents into a MongoDB database
- Query documents from a MongoDB database
- Update documents in a MongoDB database
- Delete documents from a MongoDB database
Before we can perform CRUD operations, we need to connect to a MongoDB database. We use the MongoClient.connect
method to connect to the MongoDB server.
To insert documents into a MongoDB database, we use the insertOne
or insertMany
methods. The insertOne
method inserts a single document into a collection, while the insertMany
method inserts multiple documents.
To query documents from a MongoDB database, we use the find
method. We can pass in a query filter to find specific documents.
To update a document in a MongoDB database, we use the updateOne
or updateMany
methods. We pass in a query filter to find the document(s) to update, and an update document that specifies the modifications to apply.
To delete a document from a MongoDB database, we use the deleteOne
or deleteMany
methods. We pass in a query filter to find the document(s) to delete.
Here's an example of connecting to a MongoDB database.
const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
console.log("Connected to MongoDB!");
db.close();
});
Here's an example of inserting a document into a collection.
const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
let dbo = db.db("mydb");
let myobj = { name: "John", age: "23" };
dbo.collection("users").insertOne(myobj, function(err, res) {
if (err) throw err;
console.log("1 document inserted");
db.close();
});
});
Here's an example of querying documents from a collection.
const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
let dbo = db.db("mydb");
dbo.collection("users").find({}).toArray(function(err, result) {
if (err) throw err;
console.log(result);
db.close();
});
});
Here's an example of updating a document in a collection.
const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
let dbo = db.db("mydb");
let myquery = { name: "John" };
let newvalues = { $set: {age: "25" } };
dbo.collection("users").updateOne(myquery, newvalues, function(err, res) {
if (err) throw err;
console.log("1 document updated");
db.close();
});
});
Here's an example of deleting a document from a collection.
const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
let dbo = db.db("mydb");
let myquery = { name: 'John' };
dbo.collection("users").deleteOne(myquery, function(err, obj) {
if (err) throw err;
console.log("1 document deleted");
db.close();
});
});
In this tutorial, you've learned how to perform CRUD operations in MongoDB using the MongoDB Node.js driver. You've learned how to connect to a MongoDB database, insert documents, query documents, update documents, and delete documents.
To reinforce what you've learned, try the following exercises:
Remember, practice is the key to mastering any skill!