In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
本篇文章给大家分享的是有关mongodb中怎么手动封装模块,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
mongodb操作的模块手动封装
[注]:这些代码都是基于nodejs进行的.
在nodejs目录下创建一个模块文件*.js,以便调用,
以下是模块代码:
function mongodbModel(dbname,dataform) { var MongoClient; var DB_CONN_STR; this.init = function() { MongoClient = require('mongodb').MongoClient; DB_CONN_STR = 'mongodb://localhost:27017/' + dbname; } /*这里是插入数据*/ this.insert = function(data,callback) { MongoClient.connect(DB_CONN_STR, function(err, db) { console.log('连接成功') var collection = db.collection(dataform); collection.insert(data, function(err,result){ callback(err,result); }) }) } /*这里是删除数据*/ this.remove = function(data,callback){ MongoClient.connect(DB_CONN_STR, function(err, db) { console.log('连接成功') var collection = db.collection(dataform); collection.remove(data, function(err,result){ callback(err,result); }) }) } /*这里是修改*/ this.update=function(data,updata,callback){ MongoClient.connect(DB_CONN_STR, function(err, db) { console.log('连接成功') var collection = db.collection(dataform); collection.update(data,updata,function(err,data){ callback(err,data); }) }) } /*这里是查询*/ this.find=function(data,callback){ MongoClient.connect(DB_CONN_STR, function(err, db) { console.log('连接成功') var collection = db.collection(dataform); collection.find(data).toArray(function(err,data){ callback(err,data); }) }) }}module.exports = mongodbModel;
如何使用:
在同目录下创建一个调用文件*.js
var mongodbModel = require('./mongodbModel');var mongodbObject = new mongodbModel('seraph','singer');mongodbObject.init();/*这里是插入*/mongodbObject.insert({name:'啊xx'},function(err,data){ if(err){ console.log(err); }else{ console.log(data); }})/*这是删除*/mongodbObject.remove({name:'啊xiao'},function(err,data){ if(err){ console.log(err); }else{ console.log('成功删除指定数据'); }})/*这里是修改*/mongodbObject.update({name:'小李'},{$set:{name:'小勇'}},function(err,data){ if(err){ console.log(err); }else{ console.log(data); }})/*这里是条件查询*/mongodbObject.find({name:'李洛克'},function(err,data){ if(err){ console.log(err); }else{ console.log(data); }})
相关参数:
dbname:是指要操作的database;
dataform:是指要操作的数据表;
data:表示需要插入或者查询或者修改的数据;
updata:表示修改后的数据;
个人在封装的时候,callback()是个难点
以上就是mongodb中怎么手动封装模块,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.