MongoDB数据库操作增删查改函数包装

mongodb数据库操作模块

在这里插入图片描述

简单的说就是通过构造器将mongodb复杂的中间操作省去,而直接对数据进行操作

  1. 引入mongodb模块
const mongodb = require('mongodb') 
  1. 定义mongo客户端、mongo ObjectIdMongoClient服务器ip地址
const mongoclient = mongodb.MongoClient
const objectid = mongodb.ObjectId
const mongodbUrl = 'mongodb://127.0.0.1:27017'
  1. 构建一个MongoControl构造器 变量 dbName数据库名称/tableName集合变量
mongo客户端连接基本步骤
  1. MongoClient连接mongodbUrl
    mongoclient.connect(mongodbUrl, { useNewUrlParser: true }, (err, client) = {     }    
    
    服务器,返回连接成功后的客户端
  2. 连接数据库,定义为db
    var db = client.db(this.dbName) 
    
  3. 选择当前数据库中集合
    db.collection(this.tableName)     
    
  4. 再对数据库进行各种数据操作,如insert(插入)、update(更新)、remove(移除)、find(查找)等操作

全部代码如下

const mongodb = require('mongodb')
const mongoclient = mongodb.MongoClient
const objectid = mongodb.ObjectId
const mongodbUrl = 'mongodb://127.0.0.1:27017'
var MongoControl = function (dbName, tableName) {
    this.tableName = tableName
    this.dbName = dbName
    this.insert = function (data, callback) {
        mongoclient.connect(mongodbUrl, { useNewUrlParser: true }, (err, client) => {
            if (err) {
                console.log(err)
                return
            }
            var db = client.db(this.dbName)
            db.collection(this.tableName).insert(data, function (err, res) {
                callback(err, res)
                client.close()
            })
        })
    }
    this.find = function (findQuery, callback) {
        mongoclient.connect(mongodbUrl, { useNewUrlParser: true }, (err, client) => {
            if (err) {
                console.log(err)
                return
            }
            var db = client.db(this.dbName)
            db.collection(this.tableName).find(findQuery).toArray(function (err, res) {
                callback(err, res)
                client.close()
            })
        })
    }
    this.findById = function (_id, callback) {
        mongoclient.connect(mongodbUrl, { useNewUrlParser: true }, (err, client) => {
            if (err) {
                callback(err)
                 return
            }
            var db = client.db(this.dbName)
            db.collection(this.tableName).find({ _id: objectid(_id) }).toArray(function (err, res) {
                callback(err, res)
                client.close()
            })
        })
    }
    this.removeByid = function (_id, callback) {
        mongoclient.connect(mongodbUrl, { useNewUrlParser: true }, (err, client) => {
            if (err) {
                return console.log(err)
            }
            var db = client.db(this.dbName)
            db.collection(this.tableName).remove({ _id: objectid(_id) }, function (err, res) {
                callback(err, res.result)
                client.close()
            })
        })
    }
    this.update = function (jName, xName, callback) {
        mongoclient.connect(mongodbUrl, { useNewUrlParser: true }, (err, client) => {
            if (err) {
                console.log(err)
                return 
            }
            var db = client.db(dbName)
            db.collection(tableName).update(jName, { $set: xName }, function (err, res) {
                callback(err, res.result)
                client.close()
            })
        })
    }
    this.updateById = function (_id, newdos, callback) {
        mongoclient.connect(mongodbUrl, { useNewUrlParser: true }, (err, client) => {
            if (err) {
                return console.log(err)
            }
            var db = client.db(dbName)
            db.collection(tableName).update({ _id: objectid(_id) }, { $set: newdos }, function (err, res) {
                callback(err, res.result)
                client.close()
            }) 
        })
    }
} // 引出mongocontrol模块
exports.MongoControl = MongoControl                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

在处理数据时会存在的一些问题:

  1. 插入数据时需用toArray()方法,将集合转化为数组。
  2. mongodb id处理必须用其自身所带的属性ObjectId进行操作。
  3. 在对数据进行update方法更新时必须使用{$set:nowdate}的方式进行更新,否则会覆盖当前数据
  4. 通过callback回调函数处理结果。\r\n\u003e5. 处理完结果需关闭客户端,否则服务器会一直处于等待状态。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值