Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 使用Mongo shell计算每个给定日期和文档列表的集合数_Mongodb - Fatal编程技术网

Mongodb 使用Mongo shell计算每个给定日期和文档列表的集合数

Mongodb 使用Mongo shell计算每个给定日期和文档列表的集合数,mongodb,Mongodb,我在Mongo DB中有以下收藏: 2019年11月1日 2019年11月2日 2019年11月3日 2019年11月4日 2019年11月5日 2019年11月6日 2019年11月7日 2019年11月8日 2019年11月9日 BBB-01-2019年11月 BBB-02-2019年11月 BBB-03-2019年11月 BBB-04-2019年11月 BBB-05-2019年11月 BBB-06-2019年11月 BBB-07-2019年11月 BBB-08-2019年11月 BBB-

我在Mongo DB中有以下收藏:

  • 2019年11月1日
  • 2019年11月2日
  • 2019年11月3日
  • 2019年11月4日
  • 2019年11月5日
  • 2019年11月6日
  • 2019年11月7日
  • 2019年11月8日
  • 2019年11月9日
  • BBB-01-2019年11月
  • BBB-02-2019年11月
  • BBB-03-2019年11月
  • BBB-04-2019年11月
  • BBB-05-2019年11月
  • BBB-06-2019年11月
  • BBB-07-2019年11月
  • BBB-08-2019年11月
  • BBB-09-2019年11月
我还有一份日期清单:

日期=[“2019年11月1日”、“2019年11月3日”、“2019年11月5日”、“2020年1月1日”]

我想:

  • 仅针对my_dates中的日期统计集合AAA和BBB中的文档数

  • 如果my_dates中的日期没有集合AAA或BBB(可以是count=0或特定集合不存在的文本),则会收到通知

  • 计算AAA和BBB的数量,以清楚地看到它等于len(我的约会)(因为len(我的约会)可以超过100)

我可以计算以AAA或BBB开头的集合的文档数,如下所示:

db.getCollectionNames().forEach(function(collection) 
{
  if(collection.startsWith("AAA") || collection.startsWith("BBB"))
  {
    resultCount = db[collection].count();
    print("Results count for " + collection + ":\t"+ resultCount);
    }
  });
if(collection.startsWith("AAA"+"-01-Oct-2019"))
向集合名称添加日期的操作如下:

db.getCollectionNames().forEach(function(collection) 
{
  if(collection.startsWith("AAA") || collection.startsWith("BBB"))
  {
    resultCount = db[collection].count();
    print("Results count for " + collection + ":\t"+ resultCount);
    }
  });
if(collection.startsWith("AAA"+"-01-Oct-2019"))

但我不知道如何使用我的日期中的元素进行迭代。

看看此解决方案是否满足您的要求:

您可以使用
.forEach
方法或使用
for
运算符迭代
my_dates
数组:

for(var i=0; i<my_dates.length; i++) {
    print(my_dates[i])
}

第二个解决方案挂起在我的intellishell中。@Piotrek挂起它可能是因为
.count()
花费太多时间。尝试修改脚本,不要计算总值