1、要使用mongodb数据库首先需要下载安装mongodb
官网下载:https://docs.mongodb.com/
2、上模块代码(modlue.js)
let mongodb =require('mongodb');//引进数据库模块
let mongoClient = mongodb.MongoClient;
const url = 'mongodb://127.0.0.1:27017';
class Db{
/*
@database 数据库名字
@colName 集合名字
*/
constructor(database,colName){
this.database = database;
this.colName = colName;
}
/*
查询数据
@obj 查询的对象
@cb 回调函数
*/
find(obj,cb){
let that = this;
mongoClient.connect(url,function(err,dbs){
if(err){ //判断是否出错,err为null则成功
console.log(err);
}else{
let db = dbs.db(that.database);
db.createCollection(that.colName).then(function(collection){
collection.find(obj).toArray(function(err,data){
dbs.close(); //关闭数据库
if(err){
console.log(err);
}else{
typeof cb =='function' && cb(data);
}
});
})
}
})
}
/*
插入一条数据
@obj 查询的对象
@cb 回调函数
*/
insertOne(obj,cb){
let that = this;
mongoClient.connect(url,function(err,dbs){
if(err){
console.log(err);
}else{
let db = dbs.db(that.database);
db.createCollection(that.colName).then(function(collection){
collection.insertOne(obj,function(err){
dbs.close();
if(err){
console.log(err);
}else{
typeof cb =='function' && cb();
}
});
})
}
})
}
/*
插入多条数据
@obj 查询的对象
@cb 回调函数
*/
insertMany(obj,cb){
let that = this;
mongoClient.connect(url,function(err,dbs){
if(err){
console.log(err);
}else{
let db = dbs.db(that.database);
db.createCollection(that.colName).then(function(collection){
collection.insertMany(obj,function(err){
dbs.close();
if(err){
console.log(err);
}else{
typeof cb =='function' && cb();
}
});
})
}
})
}
/*
删除一条数据
@obj 查询的对象
@cb 回调函数
*/
deleteOne(obj,cb){
let that = this;
mongoClient.connect(url,function(err,dbs){
if(err){
console.log(err);
}else{
let db = dbs.db(that.database);
db.createCollection(that.colName).then(function(collection){
collection.deleteOne(obj,function(err){
dbs.close();
if(err){
console.log(err);
}else{
typeof cb =='function' && cb();
}
});
})
}
})
}
/*
删除多条数据
@obj 查询的对象
@cb 回调函数
*/
deleteMany(obj,cb){
let that = this;
mongoClient.connect(url,function(err,dbs){
if(err){
console.log(err);
}else{
let db = dbs.db(that.database);
db.createCollection(that.colName).then(function(collection){
collection.deleteMany(obj,function(err){
dbs.close();
if(err){
console.log(err);
}else{
typeof cb =='function' && cb();
}
});
})
}
})
}
/*
更新一条数据
@obj 查询的对象
@obj1 更改内容
@cb 回调函数
*/
update(obj,newobj,cb){
let that = this;
mongoClient.connect(url,function(err,dbs){
if(err){
console.log(err);
}else{
let db = dbs.db(that.database);
db.createCollection(that.colName).then(function(collection){
collection.update(obj,newobj,function(err){
dbs.close();
if(err){
console.log(err);
}else{
typeof cb =='function' && cb();
}
});
})
}
})
}
/*
更新多条数据
@obj 查询的对象
@obj1 更改内容
@cb 回调函数
*/
updateMany(obj,obj1,cb){
let that = this;
mongoClient.connect(url,function(err,dbs){
if(err){
console.log(err);
}else{
let db = dbs.db(that.database);
db.createCollection(that.colName).then(function(collection){
collection.updateMany(obj,obj1,function(err){
dbs.close();
if(err){
console.log(err);
}else{
typeof cb =='function' && cb();
}
});
})
}
})
}
}
module.exports = Db; //暴露
3、使用模块代码(use.js)
let Db = require('./modlue.js'); //自定义引进模块
let mongodb = new Db('test','user');//实例对象,并传参
mongodb.insertOne({name:'丫1',age:'20'},function(){
mongodb.find({},function(data){
console.log(data);
});
})
然后可以开启MongoDB服务后,执行:node use.js