How can I use mongodump to dump out records matching a specific date range?

后端 未结 8 415
悲哀的现实
悲哀的现实 2021-01-30 10:45

I\'m trying to use the mongodump command to dump out a bunch of records created on a specific date. The records include a \"ts\" field which is a MongoDB Date() object.

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-30 11:04

    Edit: fixed typos

    Add an update:

    1. mongodump --query doesn't support IsoDate, but accepts Date in milliseconds form.

    2. As date command behaves different in OS X, date -d 2011-08-10 +%s does not work for me. If you've run into the same issue, try to read the manual or use this:

      • Get current time in seconds:

        date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"
        
      • Get specific time in seconds:

        date -j -f "%Y-%m-%d %H:%M:%S" "2014-01-01 00:00:00"  "+%s"
        
    3. Use the single quote version to avoid escaping.

      mongodump --query '{updated_at: { $gte: Date(1403280000000) } }'
      

提交回复
热议问题