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

前端 未结 6 667
感动是毒
感动是毒 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:20

    try this:

    NUM=10000    
    doc=selected_doc
    taskid=$(mongo 127.0.0.1/selected_db -u username -p password --eval "db.${doc}.find({}, {_id: 1}).sort({_id: -1}).skip($NUM).limit(1)" |  grep -E  -o '"[0-9a-f]+"')
    mongodump --collection $doc  --db selected_db --host 127.0.0.1 -u username -p password -q "{_id: {\$gte: $taskid}}" --out ${doc}.dump
    

提交回复
热议问题