Skip to content

Instantly share code, notes, and snippets.

@izmailoff
Created November 27, 2019 05:42
Show Gist options
  • Save izmailoff/386d4d606632208a91d62cda4cae451f to your computer and use it in GitHub Desktop.
Save izmailoff/386d4d606632208a91d62cda4cae451f to your computer and use it in GitHub Desktop.
Mongo shell script to get latest docs in each collection
var cols = db.getCollectionNames();
cols.forEach(x => {
var res = db[x].find({}).sort({_id: -1}).limit(3);
print(x + ": ");
res.forEach(x => print(x._id + " is " + x._id.getTimestamp()));
print();
}
);
@izmailoff
Copy link
Author

Prints:

collectionA: 
58edb48240f272f404ca1017 is Wed Apr 12 2017 05:00:50 GMT+0000 (UTC)
58edb46840f272f404ca0fc8 is Wed Apr 12 2017 05:00:24 GMT+0000 (UTC)
58edb46840f272f404ca0fc7 is Wed Apr 12 2017 05:00:24 GMT+0000 (UTC)

collectionB: 
5dde0c3a276671f7590dea3d is Wed Nov 27 2019 05:40:10 GMT+0000 (UTC)
5dde0c29276671f7590dea3b is Wed Nov 27 2019 05:39:53 GMT+0000 (UTC)
5dde0c18276671f7590dea39 is Wed Nov 27 2019 05:39:36 GMT+0000 (UTC)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment