5xuh_a
Last Updated: February 25, 2016
·
56
· fsamir
2b25095a2e4a0aa4dfe0cd854006bb8e

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);
    }
});
Say Thanks
Respond