How to use mongoexport with query script file

lior picture lior · Aug 12, 2014 · Viewed 7.4k times · Source

I'm trying to follow this tutorial: http://www.ultrabug.fr/tag/mongoexport/

and use a sh file for the query line. this is my file:

#!/bin/bash
d=`date --date="-3 month"`
echo "{ timeCreated: { "\$lte": $d} }"

this is my mongoexport line:

 mongoexport --db game_server --collection GameHistory -query /home/dev/test2.sh --out /home/dev/file.json

I keep getting:

assertion: 16619 code FailedToParse: FailedToParse: Expecting '{': offset:0 of:/home/dev/test2.sh

why? How can I make this work?

Answer

Leonid Beschastny picture Leonid Beschastny · Aug 12, 2014

I found several errors in your approach, let's examine them one by one.

Date format

MongoDB expects date to be a number or ISO 8601 string.

Unfortunately, unix date utility have no build-in support for this format, so you should use:

d=`date --date="-3 month" -u +"%Y-%m-%dT%H:%M:%SZ"`

Using extended JSON

JSON specification have no support for dates, so you should use MongoDB extended JSON. So, your final query should look like this:

{ "timeCreated": { "$lte": { "$date": "2014-05-12T08:53:29Z" } } }

test.sh output

You messed up with quotation marks. Here is a script example, outputting correct JSON:

#!/bin/bash
d=`date --date="-3 month" -u +"%Y-%m-%dT%H:%M:%SZ"`
echo '{ "timeCreated": { "$lte": { "$date": "'$d'" } } }'

Passing query to mongoexport

mongoexport expects --query to be a JSON string, not .sh script. So, when you're passing file path to --query, mongoexport expects it to be a JSON file.

To fix it you should execute test2.sh yourself and pass resulting string to mongoexport:

mongoexport --db game_server --collection GameHistory \
  --query "`./test2.sh`" --out ./test2.json

N.B. Notice " quotation marks around ./test2.sh call. They're telling bash to treat ./test2.sh output as a single parameter, ignoring all inner quotation marks and whitespaces.