Node. js operates the mongodb addition, deletion, modification, and query function instance, nodejsmongodb
This article describes the addition, deletion, modification, and query functions of node. js operations on mongodb. We will share this with you for your reference. The details are as follows:
Install related modules
If this is used, you need to install the required modules on your own and enter
npm install mongodb --save
After installing the module, you can perform the following steps.
File Introduction
The following is the relevant code I wrote and put it in the relevant directory that you can reference. I put it to the express root directory.
Function Mongo (options) {this. settings = {url: 'mongodb: // localhost: 27017/jk ', MongoClient: require ('mongodb '). expose client, assert: require ('assert ')}; for (let I in options) {this. settings [I] = options [I];} this. _ run = function (fun) {let that = this; let settings = this. settings; this. settings. consumer Client. connect (this. settings. url, function (err, db) {settings. assert. equal (null, err); console. Log ("Connected correctly to server"); fun (db, function () {db. close () ;}) ;};}; this. insert = function (collectionName, data, func) {// Add data let insertDocuments = function (db, callback) {let collection = db. collection (collectionName); collection. insertMany ([data], function (err, result) {if (! Err) {func (true) ;}else {func (false) ;}callback (result) ;}; this. _ run (insertDocuments) ;}; this. update = function (collectionName, updateData, data, func) {// update data let updateDocument = function (db, callback) {let collection = db. collection (collectionName); collection. updateOne (updateData, {$ set: data}, function (err, result) {if (! Err) {func (true) ;}else {func (false) ;}callback (result) ;}; this. _ run (updateDocument) ;}; this. delete = function (collectionName, data, func) {// delete data let deleteDocument = function (db, callback) {let collection = db. collection (collectionName); collection. deleteOne (data, function (err, result) {if (! Err) {func (true) ;}else {func (false) ;}callback (result) ;}; this. _ run (deleteDocument) ;}; this. find = function (collectionName, data, func) {// query data let findDocuments = function (db, callback) {// Get the documents collection let collection = db. collection (collectionName); // Find some documents collection. find (data ). toArray (function (err, docs) {if (! Err) {func (true, docs) ;}else {func (false, err) ;}callback (docs) ;}; this. _ run (findDocuments) ;};} module. exports = Mongo;
I saved it to a file named server. js.
Use
We need to introduce the module first when using the page. For example, I introduce the module in the Routing File index. js:
const Server = require("../server.js");
Then you need to instantiate the object as follows:
let server = new Server();
If you need to configure relevant information, you can input an object configuration during instantiation, you can configure the address of the database:
let server = new Server({url:"mongodb://localhost:27017/mydb"});
It encapsulates four methods and adds deletion and modification queries, which are
Add Method
Server. insert (data table name, data to be inserted (key-Value Pair object), callback function );
Update Method
Server. update (data table name, queried data (object), updated data (object), callback function );
Delete Method
Server. delete (data table name, queried data (object), callback function );
Search Method
Server. find (data table name, queried data (object), callback function );
The callback function returns two values, the first boolean type, whether the processing is successful, the second value, and the number of searched values, returns the number of successfully processed items (only one record is processed at a time)
Use Cases
For example, if I need to search for data in a route, I need:
Server. find ("users", {username: "username"}, function (bool, data) {if (bool) {console. log ("the queried data is" + data. length + "entries");} else {console. log (data );}});});
The code above queries the data of the username field in the users table. If the data is successful, an array is returned for the subsequent data. If an error occurs, a data error is returned directly.
I hope this article will help you design nodejs programs.