简体   繁体   中英

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.

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);
    }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM