Can you use mongodump to dump the latest \"x\" documents from a collection? For example, in the mongo shell you can execute:
db.stats.find().sort({$natural:-1}).
_id
-based approaches may not work if you use a custom _id
for your collection (such as returned by a 3rd party API). In that case, you should depend on a createdAt
or equivalent field:
COL="collectionName"
HOW_MANY=10000
DATE_CUTOFF=$(mongo dbname --quiet \
--eval "db.$COL.find({}, { createdAt: 1 }).sort({ createdAt: -1 }).skip($HOW_MANY).limit(1)"\
| grep -E -o '(ISODate\(.*?\))')
echo "Copying $HOW_MANY items after $DATE_CUTOFF..."
mongodump -d dbname -c ${COL}\
-q "{ createdAt: { \$gte: $DATE_CUTOFF} }" --gzip