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

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

    I was playing with a similar requirement (using mongodump) where I wanted to do sequential backup and restore. I would take dump from last stored timestamp. I couldn't get through --query '{ TIMESTAMP : { $gte : $stime, $lt : $etime } }'

    Some points to note: 1) use single quote instead of double 2) do not escape $ or anything 3) replacing $stime/$etime with real numbers will make the query work 4) problem I had was with getting $stime/$etime resolved before mongodump executes itself under -x it showed as + eval mongodump --query '{TIMESTAMP:{\$gte:$utc_stime,\$lt:$utc_etime}}' ++ mongodump --query '{TIMESTAMP:$gte:1366700243}' '{TIMESTAMP:$lt:1366700253}'

    Hell, the problem was evident. query gets converted into two conditionals.

    The solution is tricky and I got it after repeated trials.... escape { and } ie use { ..} . This fixes the problem.

提交回复
热议问题