根据Mongo中的列值返回集合名称 [英] Return Collection names based on column value in Mongo
问题描述
我有5个集合C1,C2,C3,C4和N5,每个集合都有一个last_updated值.
I have 5 collections C1, C2, C3, C4 and N5 each having a last_updated value.
db.getCollectionNames().filter(function(c){ return c.indexOf('C')==0})
将返回所有以"C"开头的集合.
will return me all the collections that start with "C".
是否可以前进一步,并基于"last_updated"列进行过滤,以便我将获得昨天更新的所有"C"集合.
Is it possible to go one step ahead and do a filter based on "last_updated" column so that i will get all "C" collections that are updated yesterday.
类似的东西
db.getCollectionNames().filter(function(c){ return (db.c.find({update_on: "01-Dec-2014"}).count()
> 1)});
尽管查询运行良好,但没有产生任何结果.
Eventhough the query runs fine its not yeilding any result.
推荐答案
即使查询日期集合中存在一条记录,也需要返回true
.
You need to return true
even if one record is present is present in the collection for the queried date.
第二,db.c
解析为值c
,因此找到了一个不存在的名为c
的集合的计数.要将c
解析为变量,我们需要将其用作db[c]
.
Second, db.c
is resolved to value c
,Therefore finding the count of a collection named c
, which does not exist. To resolve c
as a variable, we need to use it as db[c]
.
下面修改的代码可以做到这一点:
The modified code below would do that:
db.getCollectionNames().filter(function(c){
var rec = db[c].count({update_on: "01-Dec-2014"});
return (rec > 0);
})
这篇关于根据Mongo中的列值返回集合名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!