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

前端 未结 6 709
感动是毒
感动是毒 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条回答
  •  旧时难觅i
    2021-01-31 09:16

    Building off of Mic92's answer, to get the most recent 1000 items from a collection:

    Find the _id of the 1000th most recent item:

    db.collection.find('', {'_id':1}).sort({_id:-1}).skip(1000).limit(1)

    It will be something like 50ad7bce1a3e927d690385ec.

    Then pass this _id in a query to mongodump:

    $ mongodump -d 'your_database' -c 'your_collection' -q '{"_id": {"$gt": {"$oid": "50ad7bce1a3e927d690385ec"}}}'

提交回复
热议问题