Home >Web Front-end >Front-end Q&A >How to write add, delete, modify, check in nodejs
In recent years, more and more developers have begun to choose to use Node.js for web development. Compared with languages such as PHP and Python, Node.js has more powerful asynchronous operations and high concurrency processing capabilities, allowing developers to build high-performance web applications more efficiently.
However, Node.js development is not easy for beginners. In this article, we will introduce how to use Node.js to perform CRUD operations. These operations are known as CRUD operations and are the core operations of any web application, so mastering them is crucial for beginners.
Before performing addition, deletion, modification and query operations, you need to prepare the Node.js operating environment and database. The database used in this article is MongoDB, which is a document database commonly used in Node.js web development.
You can download and install MongoDB through the official website, or use MongoDB through a cloud service provider (such as MongoDB Atlas or Amazon DocumentDB). This article will not introduce the installation and configuration of MongoDB in detail. Readers can find relevant information to learn by themselves.
In Node.js, connecting to the MongoDB database requires the use of the officially provided MongoDB driver. First, we need to install the MongoDB driver, using the following command:
npm install mongodb --save
Then, we can connect to the MongoDB database through the following code. Note that the following example code assumes MongoDB is running on localhost. If you are using a remote MongoDB instance, you will need to replace localhost
with the IP address or hostname of the instance.
const MongoClient = require("mongodb").MongoClient; const url = "mongodb://localhost:27017/mydb"; MongoClient.connect(url, function (err, db) { if (err) throw err; console.log("数据库已连接!"); db.close(); });
In the above code, we use the MongoClient
object to connect to the MongoDB database and specify the connection URL. If the connection is successful, a prompt message is output, and then the database connection is closed.
In MongoDB, data is stored in the form of documents. Each document is a JSON object that can contain various properties and values. To insert data into a collection, we can use the insertOne()
or insertMany()
method. The following is the sample code of the insertOne()
method:
const MongoClient = require("mongodb").MongoClient; const url = "mongodb://localhost:27017/mydb"; MongoClient.connect(url, function (err, db) { if (err) throw err; const dbo = db.db("mydb"); const myobj = { name: "菜鸟教程", url: "www.runoob.com" }; dbo.collection("sites").insertOne(myobj, function (err, res) { if (err) throw err; console.log("文档插入成功"); db.close(); }); });
In the above code, we have used the dbo.collection()
method to get the collection object and use insertOne()
method inserts a document into the collection. If the insertion is successful, a prompt message is output, and then the database connection is closed.
In MongoDB, you can use the find()
method to query documents. find()
The method returns a cursor that contains all documents that meet the query conditions. The following is the sample code for the find()
method:
const MongoClient = require("mongodb").MongoClient; const url = "mongodb://localhost:27017/mydb"; MongoClient.connect(url, function (err, db) { if (err) throw err; const dbo = db.db("mydb"); dbo.collection("sites").find({}).toArray(function (err, result) { if (err) throw err; console.log(result); db.close(); }); });
In the above code, we have used the find()
method to query all the documents in the collection and Use the toArray()
method to convert query results into an array. If the query is successful, the query results are output, and then the database connection is closed.
If you only want to query documents that meet specific conditions, you can pass in a JSON object containing the query conditions in the find()
method, for example:
dbo.collection("sites").find({ name: "菜鸟教程" }).toArray(function (err, result) { // ... });
The above code will query the document named "Rookie Tutorial" and return all documents that meet the conditions.
To update data in MongoDB, you can use the updateOne()
or updateMany()
method. The following is a sample code for the updateOne()
method:
const MongoClient = require("mongodb").MongoClient; const url = "mongodb://localhost:27017/mydb"; MongoClient.connect(url, function (err, db) { if (err) throw err; const dbo = db.db("mydb"); const myquery = { name: "菜鸟教程" }; const newvalues = { $set: { name: "Runoob" } }; dbo.collection("sites").updateOne(myquery, newvalues, function (err, res) { if (err) throw err; console.log("文档更新成功"); db.close(); }); });
In the above code, we have used the updateOne()
method to update a document in the collection. First, we use the myquery
object to specify the document to update, and then we use the newvalues
object to specify the new values. The $set
operator will modify existing fields or add new fields. If the update is successful, a prompt message is output, and then the database connection is closed. If you want to update multiple documents that match specific criteria, you can use the updateMany()
method.
In MongoDB, you can use the deleteOne()
or deleteMany()
method to delete documents. The following is a sample code for the deleteOne()
method:
const MongoClient = require("mongodb").MongoClient; const url = "mongodb://localhost:27017/mydb"; MongoClient.connect(url, function (err, db) { if (err) throw err; const dbo = db.db("mydb"); const myquery = { name: "菜鸟教程" }; dbo.collection("sites").deleteOne(myquery, function (err, obj) { if (err) throw err; console.log("文档删除成功"); db.close(); }); });
In the above code, we have used the deleteOne()
method to delete a document in the collection. First, we specify the document to be deleted using the myquery
object. If the deletion is successful, a prompt message will be output, and then the database connection will be closed. If you want to delete multiple documents that match specific criteria, you can use the deleteMany()
method.
This article introduces how to use Node.js to perform add, delete, modify and query operations on the MongoDB database, which is the so-called CRUD operation. In actual development, you need to master more basic knowledge of Node.js and MongoDB database management skills to complete more complex operations. I hope this article can be helpful to beginners.
The above is the detailed content of How to write add, delete, modify, check in nodejs. For more information, please follow other related articles on the PHP Chinese website!