Shell script mongodump "positional arguments not allowed"

Hi i’ve created the following script to mongodump:

QUERY=" { “_id” : { “$gt” : ObjectId(“61b2346a0000000000000000”) } }"

for col in “${COLLECTIONS[@]}”;
do
    cmd=“mongodump --db=$db --collection=$col --query=’ ${QUERY} ’ -- 
             archive=/tmp/bkpmongodev/$FOLDER-$db-$col.gz --gzip”
     echo $($cmd)
     echo $cmd
done

The first echo i run the string cmd and it returns:

positional arguments not allowed: [{ “_id” : { “$gt” : ObjectId(“61b2346a0000000000000000”) } } ']

but when i copy the cmd (2º echo) and paste in terminal, it works

i tried mongoexport too and returned the almost the same

too many positional arguments: [{ “_id” : { “$gt” : ObjectId(“61b2346a0000000000000000”) }}’

1 Like

the error occurred because of spaces between strings in $QUERY

Before:


> QUERY=" { \"_id\" : { \"\$gt\" : ObjectId(\"$stringObjectId\") } } "

Now:


> QUERY="{_id:{\$gt:ObjectId(\"$stringObjectId\")}}"

2 Likes