Search code examples
mongodbmongodump

mongodump with query on slave db


I'm trying to dump from my secondary so I don't hurt the live traffic.

I can dump from it successfully. but if I add a query I get 0 documents.

What do I need to add to query succesfully to my slave?

0 Documents:
mongodump --host xxxx --port 27017 --username xxx --password xxx --db test --collection xxxx --query "{date_visited:{'$gte':new Date(1451606400000)}}" --out /dumps/xxxx-after-2016-01-01.json

All Documents:
mongodump --host xxxx --port 27017 --username xxx --password xxx --db test --collection xxxx --out /dumps/xxxx.json

Solution

  • Try it like that, i.e. swap the usages of double and single quotes to avoid any evaluation by the shell as noted in the docs

    --query , -q

    Provides a JSON document as a query that optionally limits the documents included in the output of mongodump.

    You must enclose the query in single quotes (e.g. ') to ensure that it does not interact with your shell environment.


    mongodump --host xxxx --port 27017 --username xxx --password xxx --db test --collection xxxx --query '{date_visited:{"$gte":new Date(1451606400000)}}' --out /dumps/xxxx-after-2016-01-01.json