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

Viewed 57309

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.

mongodump takes a -q argument which can be used to run a query to select the records to be included in the dump. Unfortunately, the -q argument needs to be provided in JSON, and it's not clear how to express a "less-than-this-date, more-than-this-date" query in pure JSON (normally such queries would use a 'new Date()' constructor)"

Any tips? I've tried using the {$date: unix-timestamp-in-milliseconds} format but it's not working for me.

8 Answers

In my case I queried entries created 14 days ago and end up with this bash script:

#!/bin/bash
date_now=`date +%s%3N`
date_2weeks_ago=$[date_now - 14 * 24 * 60 * 60 * 1000]
query=$(printf '{ createdAt: { $gte: Date(%d) } }' $date_2weeks_ago)
echo $query > query.json
mongodump \
--collection=data \
--queryFile=query.json
rm query.json

mongodump version: r4.0.12

Related