Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all collections in mongodb with specific field

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.

like image 405
kiranramanna Avatar asked Apr 08 '16 04:04

kiranramanna


Video Answer


1 Answers

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);
    }
})
like image 88
Charlino Avatar answered Nov 14 '22 23:11

Charlino