Home >Web Front-end >Front-end Q&A >How nodejs operates mongodb to add, delete, modify and check
With the gradual development of front-end technology, Node.js has gradually become one of the necessary skills for front-end developers. Operating the MongoDB database in Node.js is also an inevitable part of daily development. So, how to perform addition, deletion, modification and query operations in MongoDB in Node.js?
1. Install the MongoDB driver
Before operating MongoDB in Node.js, we need to install the MongoDB driver first. It can be installed directly via npm.
npm install mongodb --save
2. Connect to the database
Before using MongoDB, we need to establish a connection with the database first.
const MongoClient = require('mongodb').MongoClient; // 数据库地址 const dbUrl = 'mongodb://localhost:27017/'; // 指定数据库名称 const dbName = 'test'; // 建立连接 MongoClient.connect(dbUrl, function(err, client) { if (err) { console.log(err); return; } console.log('连接数据库成功'); // 获取数据库 const db = client.db(dbName); // 关闭连接 client.close(); });
3. Insert data
In MongoDB, we use collection
to represent a collection. You can obtain a collection through the db.collection
method.
const collectionName = 'users'; const insertData = { name: 'Tom', age: 18 }; db.collection(collectionName).insertOne(insertData, function(err, res) { if (err) { console.log(err); return; } console.log('插入数据成功'); client.close(); })
4. Querying data
Querying data is one of the most commonly used operations in MongoDB. We can query the data in the database through the collection.find
method. The find
method returns a reference to the Cursor
object. We need to traverse the Cursor
object to obtain all query results.
const collectionName = 'users'; const findQuery1 = { name: 'Tom' }; const findQuery2 = { age: { $lt: 30 } }; const fields = { _id: 0, name: 1, age: 1 }; db.collection(collectionName).find(findQuery1, fields).toArray(function(err, docs) { if (err) { console.log(err); return; } console.log(docs); client.close(); });
5. Update data
In MongoDB, we can use the collection.updateOne
method to update a piece of data in the collection. Update conditions and update content need to be specified.
const collectionName = 'users'; const filter = { name: 'Tom' }; const updateData = { $set: { age: 20 } }; db.collection(collectionName).updateOne(filter, updateData, function(err, res) { if (err) { console.log(err); return; } console.log('更新数据成功'); client.close(); });
6. Deleting data
In MongoDB, we use the collection.deleteOne
method to delete an item of data in the collection. Deletion conditions need to be specified.
const collectionName = 'users'; const filter = { name: 'Tom' }; db.collection(collectionName).deleteOne(filter, function(err, res) { if (err) { console.log(err); return; } console.log('删除数据成功'); client.close(); });
7. Summary
The above is the basic content of the addition, deletion, modification and query operations of the MongoDB database in Node.js. In actual development, we need to reasonably use the above methods according to different business needs to operate the database more efficiently.
The above is the detailed content of How nodejs operates mongodb to add, delete, modify and check. For more information, please follow other related articles on the PHP Chinese website!