6

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.

kiranramanna
  • 115
  • 1
  • 9
  • There is another issue regarding this. Few collections are using different naming conventions, like account_id or account or accountId or Account. Is there a way to do a regex on the column? – kiranramanna Apr 14 '16 at 22:42

1 Answers1

15

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);
    }
})
Charlino
  • 15,802
  • 3
  • 58
  • 74