• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

279
Views
Mongoexport shell script - Error in query

I wrote a shell script to create a data dump of records updated yesterday using mongoexport command.

yesterday=$(date -d 'yesterday 00:00:00' '+%s'000)
today=$(date -d 'today 00:00:00' '+%s'000)
query="'{\"updated_at\":{\$gte:new Date(${yesterday}),\$lte:new Date(${today})}}'"
echo ${query}

mongoexport -h $HOST -d $DOC -c $COL_NAME -u $USER -p $PWD -q ${query} -o $fileName

After adding query, when I run the shell script I get below error in console

'{"updated_at":{$gte:new Date(1484287200000),$lte:new Date(1484373600000)}}'
too many positional arguments: [Date(1484287200000),$lte:new Date(1484373600000)}}']
try 'mongoexport --help' for more information

When I run this query in command line it works properly. Can someone pls let me know why is this error when ran in shell script?

This works in command line.

$mongoexport -h <<HOST>> -d <<DOC>> -c <<COL> -u <<UN>> -p <<PWD>> -q '{"updated_at":{"$gte":new Date(1484287200000),"$lte":new Date(1484373600000)}}'
about 3 years ago · Santiago Trujillo
2 answers
Answer question

0

There is a rule of thumb in bash: when you use a variable, always surround it with double quotes. There are exceptions, but they are rare.

mongoexport -h "$HOST" -d "$DOC" -c "$COL_NAME" -u "$USER" -p "$PWD" -q "${query}" -o "$fileName"
about 3 years ago · Santiago Trujillo Report

0

This code worked for me

yesterday=$(date -d 'yesterday 00:00:00' '+%s'000)
today=$(date -d 'today 00:00:00' '+%s'000)
query1="{\"transactionDate\":{\$gte: new Date(${yesterday}),\$lte: new Date(${today})}}"
echo $yesterday
echo $today

mongoexport -d databasename-c collectionname --host yourip --port 27017 -p password -u username-q "${query1}" --type=csv --fields=transactionDate,amount > test5.csv
about 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error