nodejs操作mongodb的增删改查功能实例
2017-11-09 12:10
856 查看
本文实例讲述了nodejs操作mongodb的增删改查功能。分享给大家供大家参考,具体如下:
安装相关模块
如果使用这个的话,你需要先自己安装一下他需要的模块,在根目录输入
npm install mongodb --save
进行模块安装,安装成功以后就可以进行以下的步骤。
文件的引入
以下是我书写的相关代码,放到你可以引用的相关目录,本人放到了express的根目录
function Mongo(options) { this.settings = { url: 'mongodb://localhost:27017/jk', MongoClient:require('mongodb').MongoClient, 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.MongoClient.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) { //增加数据 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) { //更新数据 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) { //删除数据 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) { //查找数据 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;
我存入到了一个名字叫server.js的文件名内
使用
我们在需要使用页面先将模块引入,比如我在路由文件index.js里面引入:
const Server = require("../server.js");
然后需要实例化对象,如下:
let server = new Server();
如果需要配置相关信息,可以在实例化的时候传入一个对象配置,可以配置数据库的地址:
let server = new Server({url:"mongodb://localhost:27017/mydb"});
里面封装了四个方法,添删改查,分别是
添加方法
server.insert(数据表名,需要插入的数据(键值对的对象),回调函数);
更新方法
server.update(数据表名,查询的数据(对象),更新的数据(对象),回调函数);
删除方法
server.delete(数据表名,查询的数据(对象),回调函数);
查找方法
server.find(数据表名,查询的数据(对象),回调函数);
回调函数都会返回两个值,第一个布尔类型,是否处理成功,第二个值,查找返回查找到的个数,别的都返回处理成功的个数(现在一次只处理一条)
使用案例
比如我需要在一个路由里面查找数据,我就需要这样:
server.find("users",{username:"username"},function (bool,data) { if(bool){ console.log("查询到数据为"+data.length+"条"); } else{ console.log(data); } }); });
上面的代码是查询了users表里面username为username的字段的数据,如果成功,后面data就会返回一个数组,如果出现错误,就直接返回data错误。
希望本文所述对大家nodejs程序设计有所帮助。
您可能感兴趣的文章:
- NodeJS学习笔记之MongoDB模块
- nodejs连接mongodb数据库实现增删改查
- nodejs中使用monk访问mongodb
- 基于NodeJS+MongoDB+AngularJS+Bootstrap开发书店案例分析
- 详解nodejs操作mongodb数据库封装DB类
- NodeJS中的MongoDB快速入门详细教程
- 使用Nodejs连接mongodb数据库的实现代码
- mongoDB 多重数组查询(AngularJS绑定显示 nodejs)
- nodejs+mongodb+vue前后台配置ueditor的示例代码
- nodejs操作mongodb的填删改查模块的制作及引入实例
- nodejs+mongodb aggregate级联查询操作示例
相关文章推荐
- Nodejs操作Mongodb增删查改
- PHP操作MongoDB实现增删改查功能【附php7操作MongoDB方法】
- nodejs操作mongodb的填删改查模块的制作及引入实例
- C# 对MongoDB 进行增删改查的简单操作实例
- nodejs连接mongodb示例,实现对数据的增删改查操作(跳过不必要的坑) Windows版
- java操作mongodb实现CURD功能实例
- nodejs操作mysql实现增删改查的实例
- 使用nodejs操作mongodb来实现增删改查
- NodeJs操作mongoDB(增删查改)
- nodejs操作mongodb简单实例
- nodejs-第四天-使用MongoDB、shell增删改查操作、聚集集合操作
- MongoDB教程之查询操作实例
- Qt操作xml文件(增删改功能)
- nodejs 增删该查操作数据库(routes文件夹下的users.js)
- PHP增删改查操作实例详解
- PHP操作MongoDB(增删改查)
- MongoDB增删改查入门操作
- nodeJS学习(8)--- WS/...开发 NodeJS 项目-节3 <使用 mongodb 完整实例过程>
- MongoDB在PHP中操作实例
- 使用LitePal插件操作数据库(增删改查)讲解与实例