Skip to content
Advertisement

Export data from MongoDB with MongoExport and query

I’m trying to get a data which doesn’t start with “0”. This query works inside of MongoDB command line

db.Hikanshou.find({"number":/^(?!0)/})

but when I do this with mongoexport as

mongoexport --host MYIP --port 27017 --username "MYUSERNAME" --password "MYPASS" --authenticationDatabase "admin" --db TotsugoDataDB --collection Hikanshou --query '{"number": /^(?!0)/}' --out data.json

I’m getting an error

Failed: error parsing query as Extended JSON: invalid JSON input. Position: 17. Character: /

And with like this {"number": "/^(?!0)/"} it wont match… How could I parse that /^(?!0)/?

Advertisement

Answer

Try

mongoexport --host MYIP --port 27017 --username "MYUSERNAME" --password "MYPASS" --authenticationDatabase "admin" --db TotsugoDataDB --collection Hikanshou -q '{"number": {"$regularExpression":{"pattern":"^(?!0)", "options":""}}}' --out data.json

From the docs for mongoexport:

The query must be in Extended JSON v2 format (either relaxed or canonical/strict mode), including enclosing the field names and operators in quotes:

Basically, you need extended JSON v2, not v1.

Take a look at JSON v2 guide.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement