MongoDB
加载
mongodb
const MongoClient = require('mongodb').MongoClient; const resuleSet = require('./resultSet'); //数据库地点 const url = 'mongodb://localhost:27017'; //数据库称号 const dbName = 'user'; let db = {};
衔接数据库
MongoClient.connect(url, function(err, client) { db = client.db(dbName); });
数据库操纵
module.exports = { //collection为鸠合,filter为过滤,data为数据 //猎取数据 async select(collection, filter = {}) { //db.collection.find()该要领猎取多条数据 const result = await db.collection(collection).find(filter).toArray(); return resuleSet(result.length > 0, result); }, //增加数据 async insert(collection, data) { //db.collection.insert()该要领插进去一条或多条数据 const result = await db.collection(collection).insert(data); return resuleSet(result.length > 0, result); }, //更新数据 async update(collection, filter, data) { //db.collection.update()该要领更新多条数据 const result = await db.collection(collection).update(filter, data); return resuleSet(result.length > 0, result); }, //删除数据 async delete(collection, filter) { //db.collection.deleteOne() 该要领从鸠合中删除单个文档 const result = await db.collection(collection).deleteOne(filter); return resuleSet(result.length > 0, result); }, };