Is it possible to mongodump the last “x” records from a collection?

前端 未结 6 665
感动是毒
感动是毒 2021-01-31 08:41

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}).         


        
6条回答
  •  广开言路
    2021-01-31 09:06

    _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
    

提交回复
热议问题