Last Updated: February 25, 2016
·
260
· fsamir

Search for text value in all MongoDB collecitons

//Given a field name and search text, search it in all MongoDB collections

var collections = db.getCollectionNames();
var targetValue = "some string";

collections.forEach(function (collectionName) {
    var collection = db.getCollection(collectionName);
    var count = collection.find({"<field name>": targetValue}).count();
    if (count > 0) {
        print('Collection ' + collectionName + 'contains ' + count + ' references to ' + targetValue);
    }
});