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);
}
});
Written by Franklin Dattein
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Mongodb
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#