问题
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})
will return me all the collections that start with "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.
something like this
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.
回答1:
You need to return true
even if one record is present is present in the collection for the queried date.
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);
})
来源:https://stackoverflow.com/questions/27725626/return-collection-names-based-on-column-value-in-mongo