0

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)/?

1
  • Did you solve it? Commented Nov 18, 2020 at 6:01

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

I could successfully export data using the same regex query from Windows command prompt and MongoDB v4.2:

mongoexport --db=testdb --collection=testcoll --query="{ \"number\": { \"$regex\": \"^(?!0)\" } }" --out=testoutput.json

3 Comments

I'm trying this in ubuntu and bash says: "-bash: !0: event not found".
Try this: --query='{ "number": { "$regex": "^(?!0)" } }'
I am afraid I dont have access to linux environment to check what works correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.