Hi i have bash script for exporting mongodb data, here is the script
#!/bin/sh
DATE_NOW=$(date +%Y-%m-%dT%H:%M:%S%z)
DATE_EXPIRED=$(date +%Y-%m-%dT%H:%M:%S%z -d "$DATE_NOW - 3 month")
echo $DATE_EXPIRED
echo '{
"$or": [
{ "creationTime": { "$gt": "ISODate('\'$DATE_EXPIRED\'')" } },
{ "requestTime": { "$gt": "ISODate('\'$DATE_EXPIRED\'')"} }
]
}'
DB=nexuspay
DB_COLLECTIONS=$(mongosh "mongodb://localhost:27017/$DB?authSource=admin" --norc --quiet --eval "db.getCollectionNames().join(' ')")
for collection in $DB_COLLECTIONS; do
echo "Exporting $DB/$collection ..."
mongoexport --db=$DB --collection=$collection --query '{
"$or":[
{ "creationTime":{ "$gt":"ISODate('\'$DATE_EXPIRED\'')" } },
{ "requestTime":{ "$gt":"ISODate('\'$DATE_EXPIRED\'')"} }
]
}' --out=backup-$(date +"%d%m%Y")/$collection.json
# echo "Deleting $DB/$collection ..."
# mongosh "mongodb://localhost:27017/$DB?authSource=admin" --norc --quiet --eval "db.$collection.deleteMany({})"
done
echo "BACKUP AND CLEAN DATABASE COMPLETE"
This is the result image1 but its not what i expected, this is what the expected result in mongodb UI image2
I noticed that the query is different, but when i want to change it to the correct one (which is in image2), i got an error
2023-04-25T22:59:23.640+0800 Failed: error parsing query as Extended JSON: error decoding key 0: invalid JSON input. Position: 57. Character: I
anyone have solution? thanks
ISODate()is a function in mongo shellmongosh, it does not exist inmongoexportTry this one: