获取mongodb集合列表以及文档数量

时间:2018-07-20 04:58:12

标签: node.js mongodb asynchronous

我正在使用mongodb驱动程序连接mongo数据库。我想获取mongodb的所有数据库,以及每个集合文档的集合和计数。最后,我想将结果存储在一个JSON对象中。下面的代码为我提供了所有数据库及其集合计数,但是循环运行了多次,而在asyc.foreach时却使我感到困惑

"use strict";
var async = require("async");

// Connection url
var url = 'mongodb://localhost:27017';
var MongoClient = require('mongodb').MongoClient;
var user = 'admin';
var password = 'p2pRaw#t';
var PORT = 27017;
var IP = '@' + 'localhost' + ':' + PORT;//pass the ip  of db server here
var URL = 'mongodb://' + user + ':' + password + IP;
var result = new Set();

console.log('Connecting to server ' + IP + ' and port :' + PORT + '....\n');

function getDbNames(array) {
  var dbNames = [];
  array.forEach(function (element) {
    //avoiding system level databases name like admin and local db to get the count
    if (element.name !== 'admin' && element.name !== 'local') {
      dbNames.push(element.name);
    }
  });
  return dbNames;
}

function getCollectionsAndCount(dbName, dbConnectionManager, cb) {
  var countObject = {};
  var dbInstance = dbConnectionManager.db(dbName);
  // console.log('how many times it calling...', dbName);
  dbInstance.listCollections().toArray(function (err, items) {
    if (items && items.length) {
      async.forEach(items, function (item, callback) {
        // console.log('DataBase Name   : ' + dbName + ' and Collection Name : ' + item.name);
        dbInstance.collection(item.name.toString()).find({}).count(function (err, count) {
          result.add({dbName: dbName, collectionName: item.name.toString(), noOfRecords: count});
        });
        callback();
      }, function (err) {
        if (err) return cb(err, null);
        //returning the data have Enjoy with it...
        console.log('final Result', result);
        cb(null, result);
      });
    }
    //dbInstance.close();
  });
  // cb(null, result);
}

function iterateDatabases(dbs, dbConnection, cb) {
  var result = null;
  var error = null;
  async.forEach(dbs, function (dbName, callback) {
    getCollectionsAndCount(dbName, dbConnection, function (err, data) {
      if (data) {
        result = data;
      } else {
        error = err;
      }
      callback();
    });
  }, function (err) {
    if (err) return cb(err, null);
    //returning the data have Enjoy with it...
    cb(error, result);
  });
}

MongoClient.connect(URL, {
  authSource: 'admin'
}, function (err, db) {
  if (err) {
    console.log("Auth Failed");
    return;
  }
  console.log("\n Hey Connected with" + IP + ':' + PORT + "!!!!");
  var adminDb1 = db.admin();
  var dbConnection = db;
  adminDb1.listDatabases(function (err, result) {
    if (result && result.databases.length) {
      var databaseNames = getDbNames(result.databases);
      iterateDatabases(databaseNames, dbConnection, function (error, data) {
        console.log('data', data);
        console.log('Error', error);
      });
    } else {
      console.log('Error to get the list of DBs  : ', err);
    }
    //finally closing the connection
    //db.close();
  });
});

想要这样的结果。

[{ dbName: 'abc', collectionName: 'level', noOfRecords: 5 },
  { dbName: 'abc',
  collectionName: 'assetSchemeSetR',
  noOfRecords: 12 },
  { dbName: 'xyz',
  collectionName: 'colorSetR',
  noOfRecords: 17 },
  { dbName: 'xyz',
  collectionName: 'outcomeR',
  noOfRecords: 0 },]

1 个答案:

答案 0 :(得分:1)

下面是用于获取所有数据库及其收集列表和每个收集计数的代码块。

 (function () {
      "use strict";
      var async = require("async");

    // Connection properties
      var MongoClient = require('mongodb').MongoClient;
      var finalOutPut = [];
      var URL = 'mongodb://';
      var userName = 'admin',//will connect with admin so will get all other authentication
          password = '***', //password
          hostName = 'localhost', //ip Address
          portNumber = 27017, //portNumber
          dbName = 'testDB'; //optional
      console.log('Connecting to server... : ', URL);

      function getDbNames(array) {
        var dbNames = [];
        array.forEach(function (element) {
          //avoiding system level databases name like admin and local db to get the count
          if (element.name !== 'admin' && element.name !== 'local') {
            var key = element.name.toString(),
                obj = {
                  [key]: []
                };
            finalOutPut.push(obj);
            dbNames.push(element.name);
          }
        });
        return dbNames;
      }


      function getCollectionsAndCount(dbName, dbConnectionManager, cb) {
        var dbInstance = dbConnectionManager.db(dbName);
        dbInstance.listCollections().toArray(function (err, items) {
          if (items && items.length) {
            async.forEachSeries(items, function (item, callback) {
              dbInstance.collection(item.name.toString()).find({}).count(function (err, count) {
                //finalOutPut.push({dbName: dbName, collectionName: item.name.toString(), noOfRecords: count});
                finalOutPut.filter(function (value, index) {
                  if (value.hasOwnProperty(dbName.toString())) {
                    finalOutPut[index][dbName.toString()].push({collectionName: item.name.toString(), noOfRecords: count});
                  }
                });
              });
              callback();
            }, function (err) {
              if (err) return cb(err, null);
              //returning the data have Enjoy with it...
              cb(null, finalOutPut);
            });
          }
        });
      }

      function iterateDatabases(dbs, dbConnection, cb) {
        var result = null;
        var error = null;
        async.forEachSeries(dbs, function (dbName, callback) {
          getCollectionsAndCount(dbName, dbConnection, function (err, data) {
            if (data) {
              result = data;
            } else {
              error = err;
            }
            callback();
          });
        }, function (err) {
          if (err) return cb(err, null);
          //returning the data have Enjoy with it...
          cb(error, result);
        });
      }


     MongoClient.connect(URL, {
        authSource: 'admin'
      }, function (err, db) {
        if (err) {
          console.log("Auth Failed............\n", err);
          return;
        }
        console.log("\n Hey Connected with  " + URL + " !!!!");
        var adminDb1 = db.admin();
        var dbConnection = db;
        var fileName = 'CollectionCount.json';
        console.log('Getting Databases..');
        adminDb1.listDatabases(function (err, result) {
          if (result && result.databases.length) {
            var databaseNames = getDbNames(result.databases);      
            iterateDatabases(databaseNames, dbConnection, function (error, data) {
              //finally closing the connection
              db.close();
              console.log('\n Got the collections count from Database:\n',data);         
          } else {
            console.log('Error to get the list of DBs  : ', err);
          }
        });
      });
    }());