From 073211bd97a6686f13ee5c6a7df9b1c349ccb11a Mon Sep 17 00:00:00 2001 From: pplokijuhyg <1162963624@qq.com> Date: Tue, 19 Nov 2019 13:37:11 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E9=80=92=E5=A2=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/mongodb.js | 53 +++++++++++++++++++++++++++++++++------ controllers/login.js | 4 +-- description/mongodb.js.md | 7 +++++- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/bin/mongodb.js b/bin/mongodb.js index 9fbf4d5..abd2524 100644 --- a/bin/mongodb.js +++ b/bin/mongodb.js @@ -1,7 +1,38 @@ var MongoClient = require('mongodb').MongoClient; var dburl; -let add = (set, data, url = dburl) => { +let idadd = async (set) => { + let id = 0 + if (set == "idadd") { + return 0; + } else { + await find('idadd', { 'set': set }).then((res) => { + if (res.data.length != 0) { + id = res.data[0].num_key + } else { + id = 1 + } + }) + } + + if (id == 1) { + await add('idadd', { set, 'num_key': 2 }) + } else if (set != "idadd") { + console.log(id) + + await update('idadd', { 'set': set }, { 'num_key': id + 1 }) + } + console.log(id) + return id + +} +let add = async (set, data, url = dburl) => { + if (set != "idadd") { + let id = await idadd(set) + data.num_key = id + } + return new Promise((resolve, reject) => { + MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) { if (err) throw err; var dbo = db.db(url.dbname); @@ -18,8 +49,16 @@ let add = (set, data, url = dburl) => { }); }) } -let adds = (set, data, url = dburl) => { +let adds = async (set, data, url = dburl) => { + + if (set != "idadd") { + for (let i in data) { + let id = await idadd(set) + data[i].num_key = id + } + } return new Promise((resolve, reject) => { + MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) { if (err) throw err; var dbo = db.db(url.dbname); @@ -36,7 +75,7 @@ let adds = (set, data, url = dburl) => { }); }) } -let remove = (set, where, url = dburl) => { +let remove = async (set, where, url = dburl) => { return new Promise((resolve, reject) => { MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) { if (err) throw err; @@ -54,7 +93,7 @@ let remove = (set, where, url = dburl) => { }); }) } -let removes = (set, where, url = dburl) => { +let removes = async (set, where, url = dburl) => { return new Promise((resolve, reject) => { if (where == "" || Object.keys(where).length == 0) { reject({ code: 1 }) @@ -75,7 +114,7 @@ let removes = (set, where, url = dburl) => { }); }) } -let find = (set, where = {}, page = 0, num = 0, url = dburl) => { +let find = async (set, where = {}, page = 0, num = 0, url = dburl) => { return new Promise((resolve, reject) => { if (num == 0) { MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) { @@ -111,7 +150,7 @@ let find = (set, where = {}, page = 0, num = 0, url = dburl) => { }) } -let update = (set, where = {}, data, defin = true, url = dburl) => { +let update = async (set, where = {}, data, defin = true, url = dburl) => { return new Promise((resolve, reject) => { MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) { if (err) throw err; @@ -132,7 +171,7 @@ let update = (set, where = {}, data, defin = true, url = dburl) => { }); }) } -let updates = (set, where = {}, data, url = dburl) => { +let updates = async (set, where = {}, data, url = dburl) => { return new Promise((resolve, reject) => { MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) { if (err) throw err; diff --git a/controllers/login.js b/controllers/login.js index d1e3a19..57c8f4b 100644 --- a/controllers/login.js +++ b/controllers/login.js @@ -6,15 +6,13 @@ var loginup = async (ctx,next) => { await dbs.find('admin').then((res)=>{ arr = res.data }) - console.timeEnd("asd") - if(arr.length == 0){ await dbs.add('admin',{name:"admin",pwd:"123456"}) await dbs.find('admin').then((res)=>{ arr = res.data }) } - + console.timeEnd("asd") //判断用户名密码 if(ctx.request.body.name == arr[0].name && ctx.request.body.pwd == arr[0].pwd){ //颁发token diff --git a/description/mongodb.js.md b/description/mongodb.js.md index 1c8114f..fd064a8 100644 --- a/description/mongodb.js.md +++ b/description/mongodb.js.md @@ -41,7 +41,7 @@ find 查询数据 ``` update 更新单条数据 ``` -参数(操作的集合名,条件,要改为的{}[,{数据库地址,数据库名}]) +参数(操作的集合名,条件,要改为的{}[,默认$set 如果其他 为flase 要改为的加操作符,{数据库地址,数据库名}]) 返回值类型 promise 成功返回 {code:0}, 失败返回 {code:1} @@ -53,3 +53,8 @@ updates 更新多条数据 成功返回 {code:0,num:修改成功的数量}, 失败返回 {code:1} ``` + +####默认 + + +默认idadd表为id存储表,num_key为id 自动递增 1开始