Return Collection names based on column value in M

2019-08-05 08:03发布

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条回答
闹够了就滚
2楼-- · 2019-08-05 08:21

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);
})
查看更多
登录 后发表回答