【问题标题】:MongoDB | How to show only non-empty collections?MongoDB |如何仅显示非空集合?
【发布时间】:2020-04-01 11:56:16
【问题描述】:

我知道如何显示所有集合,但不知道如何显示非空集合。任何提示都可能让我赞赏

【问题讨论】:

  • 到目前为止你尝试过什么?你在使用mongo cli吗?还是图书馆?我想您可以计算每个集合中的文档数量,并且只生成返回大于 0 的文档?

标签: mongodb collections mongodb-query


【解决方案1】:

要在 MongoDB 中获取/查找不包含任何文档的集合,请使用以下命令。

db.getCollectionNames().forEach(function(collection) { 
    var resultCount = db[collection].count(); 
    if(resultCount==0) { 
        print("Documents count: "+ resultCount +" for collection: "+ collection);
    } 
});

输出: Documents count: 0 for collection : CUSTOMER

在 MongoDB uncomment if condition blockre-execute the code 中获取或查找所有具有数据计数的集合名称。

db.getCollectionNames().forEach(function(collection) { resultCount = db[collection].count(); print("Documents count: "+ resultCount +" for collection "+ collection); });

输出:

Documents count: 4 for collection ADDRESS
Documents count: 0 for collection CUSTOMER
Documents count: 12 for collection EMPLOYEE_DETAILS
Documents count: 30 for collection WALLET
Documents count: 5 for collection EMPLOYEE_STATUS

【讨论】:

  • 完美答案,欣赏!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-15
  • 1970-01-01
  • 2017-04-19
相关资源
最近更新 更多