Find all collections in mongodb with specific field

拈花ヽ惹草 提交于 2019-12-07 05:42:24

问题


There is more than 40 collections in database I am currently working on. One of the major key in all the collections is "account". I need to know all such collections where there is a field called "account".

Is there a query to get or a js script which prints all such collections?

In Oracle I was using :

SELECT * FROM ALL_TAB_COLUMNS WHERE COLUMN_NAME LIKE 'account'; 

Any inputs is helpful.

Thanks in advance.


回答1:


The following mongo script will print out all collection names where at least one document contains an account field.

db.getCollectionNames().forEach(function(collname) {
    var count = db[collname].find({"account": {$exists: true}}).count();
    if (count > 0) {
      print(collname);
    }
})


来源:https://stackoverflow.com/questions/36491473/find-all-collections-in-mongodb-with-specific-field

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