const ObjectID = require('mongodb').ObjectID;
const cliLog = require('../util/cliLog');

module.exports = (()=>({
  /**
   * 插入城市列表数据
   * @param db {Db}
   * @param docs {Array}
   * @param collection {String}
   * @param callback {Function}
   * @return {Promise.<Object>}
   */
  insert(db, docs, collection, callback){
    return db
      .collection(collection)
      .insertMany(docs)
      .then(res=> {
        if (res.result.ok !== 1) return cliLog.error('插入数据失败');
        cliLog.success(`${collection}: 插入 ${res.ops.length} 条数据`);
        callback(res);
        return res.ops.length;
      });
  },
  /**
   * 查询多条数据
   * @param db {Db}
   * @param callback {Function}
   * @param collection {String}
   * @param query {Object} 查询条件
   * @return {Promise.<Array>}
   */
  findMany(db, collection, callback, query = {}){
    return db
      .collection(collection)
      .find(query)
      .toArray()
      .then(docs=> {
        cliLog.success(`${collection}: 获取 ${docs.length} 条数据`);
        callback(docs);
        return docs;
      });
  },
  /**
   * 使用 id 查询数据
   * @param db {Db}
   * @param collection {String}
   * @param callback {Function}
   * @param id {Number}
   * @return {*|Promise.<Array>}
   */
  findById(db, collection, callback, id){
    const _id = new ObjectID(id);
    return db
      .collection(collection)
      .findOne({_id})
      .then(docs=> {
        cliLog.success(`${collection}: 获取 _id:${id} 的数据`);
        callback(docs);
        return docs;
      });
  }
}))();



@@ -441,35 +441,36 @@
  1) 
-return cliLog.error('%E6%8F%92%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%B1%E8%B4%A5
+ throw MongoError('%E6%9C%AA%E6%AD%A3%E7%A1%AE%E6%89%A7%E8%A1%8C%E6%8F%92%E5%85%A5%E5%91%BD%E4%BB%A4
 ');%0A
@@ -577,27 +577,16 @@
 turn res
-.ops.length
 ;%0A      
@@ -1166,14 +1166,14 @@
 id %7B
-Number
+String
 %7D%0A  
@@ -1227,32 +1227,36 @@
 (db, collection,
+ id,
  callback, id)%7B%0A
@@ -1248,20 +1248,16 @@
 callback
-, id
 )%7B%0A    c
@@ -1468,32 +1468,32 @@
    return docs;%0A
-
       %7D);%0A  %7D%0A%7D)
@@ -1486,16 +1486,762 @@
     %7D);%0A
+  %7D,%0A  deleteMany(db, collection, query, callback)%7B%0A    if (query === %7B%7D) throw MongoError('%E5%88%A0%E9%99%A4%E6%93%8D%E4%BD%9C%E9%9C%80%E8%A6%81%E4%BC%A0%E5%85%A5%E8%BF%87%E6%BB%A4%E6%9D%A1%E4%BB%B6');%0A    return db%0A      .collection(collection)%0A      .deleteMany(query)%0A      .then(res=%3E %7B%0A        if (res.result.ok !== 1) throw MongoError('%E6%9C%AA%E6%AD%A3%E7%A1%AE%E6%89%A7%E8%A1%8C%E5%88%A0%E9%99%A4%E5%91%BD%E4%BB%A4');%0A        cliLog.success(%60$%7Bcollection%7D: %E5%88%A0%E9%99%A4 $%7Bres.deletedCount%7D %E6%9D%A1%E6%95%B0%E6%8D%AE%60);%0A        callback(res);%0A        return res;%0A      %7D)%0A  %7D,%0A  /**%0A   *%0A   * @param db %7BDb%7D%0A   * @param collections %7BString%7D%0A   * @param indexes %7BArray%7D%0A   * @param callback %7BFunction%7D%0A   * @return %7BPromise.%3CBoolean%3E%7D%0A   */%0A  indexExists(db, collections, indexes, callback)%7B%0A    return db%0A      .collection(collections)%0A      .indexExists(indexes)%0A      .then(res=%3E %7B%0A        callback(res);%0A        return res;%0A      %7D)%0A
   %7D%0A%7D))(

ADD 删除文档与判断索引是存在的快捷方法