db.collectionName.find()
db.getCollectionNames()
show collections
On Sunday, 27 September 2015 13:28:30 UTC+10, A. Jalil @AJ wrote:
I have migrated 3 nodes successfully to AWS, on the status below I don’t see any lags which is good, but how do I validate each member so I can compare it with the older member to make sure all data is in sync .
I would like to check the number of Collections in each member and the number of records in each collection, but I don’t want to go thru each collection and count, I might have over 500 collections.. I was wondering if there is script I can use to generate such a report..
Hi AJ,
To iterate all collections or databases you can write forEach()
loops in the mongo
shell.
Rather than counting each collection, it is probably easier to compare the overall stats for all databases (which includes number of collections & objects):
db.adminCommand("listDatabases").databases.forEach(
function (dblist) {
dbcheck = db.getSiblingDB(dblist.name);
// Could optionally iterate dbcheck.collectionNames() to get per-collection counts or stats
printjson(dbcheck.stats());
}
)
You could add an inner loop to iterate or count all collections if you prefer to compare at that level.
For easier comparison I would save the JavaScript code in a file (eg alldbstats.js
) and redirect the output for each member of the replica set:
mongo --quiet alldbstats.js node1:27017 > node1.json
… and then use a tool like diff
to compare the JSON files (or jsondiffpatch
if you want to get fancy).
You could also do all of the comparison by iterating the replica set in the mongo
shell, but that’s probably overkill. For more information see: Write Scripts for the mongo Shell.
Regards,
Stephen