MongoDB query multiple databases at once
The same as @chridam answer, but with small improvement to generalize the db name, and as a one-liner:
This command will count all the collection with the same name in different databases:
collectionName = "sites"; db = db.getSiblingDB("admin"); dbs = db.runCommand({ "listDatabases": 1 }).databases; dbs.forEach(function(database) { db = db.getSiblingDB(database.name); collection = db.getCollection(collectionName); print(collection + " count: " + collection.count())})
And the results are:
db_0.sites count: 0
db_1.sites count: 0
db_2.sites count: 3
db_3.sites count: 4
You can use it for different purposes just change the collectionName value, or the query in the forEach function.