Return Collection names based on column value in Mongo

邮差的信 提交于 2019-12-11 13:15:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!