MongoDB mongodump query for

I am trying to use mongodump in order to create a bson file from a specified point in the oplog. I am working with mongoDB version 4.2 on linux.

When I perform the gollowing command, all is good:

# mongodump --authenticationDatabase=admin -d local -c --query "{\"ts\":{\"\$gt\":{\"\$timestamp\":{\"t\":1629206557,\"i\":1}}}}" -u myuser-p mypassword  --port 27020  -o - > /data/db/backup/a.bson
2021-08-19T05:38:52.550+0000    writing to stdout
2021-08-19T05:38:53.935+0000    dumped 20871 documents

However, if I try to put the query string into a varialbe, the dump fails, even though the string looks the same:

# query_string="\"{\\\"ts\\\":{\\\"\\\$gt\\\":{\\\"\\\$timestamp\\\":{\\\"t\\\":${TIMESTAMP_LAST_OPLOG_ENTRY},\\\"i\\\":${INC_NUMBER_LAST_OPLOG_ENTRY}}}}}\""

# echo $query_string

mongodump --authenticationDatabase=admin -d local -c --query '$query_string' -u  octopusdba -p oct-DBA+2020  --port 27020  -o - > /data/db/backup/a.bson
2021-08-19T05:45:20.109+0000    Failed: error parsing query as Extended JSON: invalid JSON input

Trying to put the query_string in double quotes did not work either:

root@10:/# mongodump --authenticationDatabase=admin -d local -c --query "$query_string" -u  myuser-p mypassword  --port 27020  -o - > /data/db/backup/a.bson
2021-08-19T05:45:30.257+0000    error parsing command line options: invalid argument for flag `-q, --query' (expected string): invalid syntax
2021-08-19T05:45:30.257+0000    try 'mongodump --help' for more information

This does not work either:

#START_TIMESTAMP="Timestamp( 1629206557, 1 )"

# mongodump --port 27020 -u myuser-p mypassword --authenticationDatabase=admin -d local -c --query '{ \"ts\" : { \"\$gt\" \": \${START_TIMESTAMP} } }' -o - > /data/db/backup/b.bson
2021-08-19T07:32:41.864+0000    Failed: error parsing query as Extended JSON: invalid JSON input

I think it should be --queryFile when you are passing a file
–query is if you are passing the exact query

1 Like


Used this then:



echo $START_TIMESTAMP > q.js

sudo docker exec my-mongo-container /bin/bash -c “mongodump --authenticationDatabase=admin -d local -c --queryFile /data/db/backup/q.js -u myuser -p mypassword --port 27020 -o - > /data/db/backup/oplog1.bson”


Thanks @ Ramachandra_Tummala

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.