Mongodb 使用Mongo shell计算每个给定日期和文档列表的集合数
我在Mongo DB中有以下收藏: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-
- 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月
- 仅针对my_dates中的日期统计集合AAA和BBB中的文档数
- 如果my_dates中的日期没有集合AAA或BBB(可以是count=0或特定集合不存在的文本),则会收到通知
- 计算AAA和BBB的数量,以清楚地看到它等于len(我的约会)(因为len(我的约会)可以超过100)
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()
花费太多时间。尝试修改脚本,不要计算总值