3

Trying to use a query with mongoexport results in an error. But the same query is evaluated by the mongo-client without an error.

In mongo-client:

db.listing.find({"created_at":new Date(1221029382*1000)})

with mongoexport:

mongoexport -d event -c listing -q '{"created_at":new Date(1221029382*1000)}'

The generated error:

Fri Nov 11 17:44:08 Assertion: 10340:Failure parsing JSON string near: 
$and: [ { 
0x584102 0x528454 0x5287ce 0xa94ad1 0xa8e2ed 0xa92282 0x7fbd056a61c4 
0x4fca29 
mongoexport(_ZN5mongo11msgassertedEiPKc+0x112) [0x584102] 
mongoexport(_ZN5mongo8fromjsonEPKcPi+0x444) [0x528454] 
mongoexport(_ZN5mongo8fromjsonERKSs+0xe) [0x5287ce] 
mongoexport(_ZN6Export3runEv+0x7b1) [0xa94ad1] 
mongoexport(_ZN5mongo4Tool4mainEiPPc+0x169d) [0xa8e2ed] 
mongoexport(main+0x32) [0xa92282] 
/lib/libc.so.6(__libc_start_main+0xf4) [0x7fbd056a61c4] 
mongoexport(__gxx_personality_v0+0x3d9) [0x4fca29] 
assertion: 10340 Failure parsing JSON string near: $and: [ {

But doing the multiplication in Date beforehand in mongoexport:

mongoexport -d event -c listing -q '{"created_at":new Date(1221029382000)}'

works!

Why is mongo evaluating the queries differently in these two contexts?

6
  • The problem goes away when I replace the 1221029382*1000 with 1221029382000. Is this a bug? Commented Nov 14, 2011 at 16:37
  • 1
    It seems to me that mongoexport cannot evaluate computations, like your multiplication. Cannot test it myself, but test it with a simple query with a multiplication or another computation. Commented Nov 14, 2011 at 17:35
  • That may be true, but then it's a bug. Why would the client process a JSON query differently than mongoexport? Commented Nov 14, 2011 at 21:40
  • I do not know if mongoexport uses the same base as the mongo shell client. I tried it now myself and it seems that mongoexport is not using a JavaScript interpreter like the mongo client. So it cannot evaluate the JSON. It only parses to build a query. Commented Nov 15, 2011 at 8:41
  • Hmm, maybe I should change the question to: "Why does mongoDB process queries differently in different contexts?" Commented Nov 16, 2011 at 21:26

2 Answers 2

8

The mongoexport command-line utility supports passing a query in JSON format, but you are trying to evaluate JavaScript in your query.

The JSON format was originally derived from JavaScript's object notation, but the contents of a JSON document can be parsed without eval()ing it in a JavaScript interpreter.

You should consider JSON as representing "structured data" and JavaScript as "executable code". So there are, in fact, two different contexts for the queries you are running.

The mongo command-line utility is an interactive JavaScript shell which includes a JavaScript interpreter as well as some helper functions for working with MongoDB. While the JavaScript object format looks similar to JSON, you can also use JavaScript objects, function calls, and operators.

Your example of 1221029382*1000 is the result of a math operation that would be executed by the JavaScript interpreter if you ran that in the mongo shell; in JSON it's an invalid value for a new Date so mongoexport is exiting with a "Failure parsing JSON string" error.

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

Comments

1

I also got this error doing a mongoexport, but for a different reason. I'll share my solution here though since I ended up on this SO page while trying to solve my issue.

I know it has little to do with this question, but the title of this post brought it up in Google, so since I was getting the exact same error I'll add an answer. Hopefully it helps someone.

I was trying to do a MongoId _id query in the Windows console. The problem was that I needed to wrap the JSON query in double quotes, and the ObjectId also had to be in double quotes (not single!). So I had to escape the ObjectId quotes.

mongoexport -u USERNAME -pPASSWORD -d DATABASE -c COLLECTION --query "{_id : ObjectId(\"5148894d98981be01e000011\")}"

If I wrap the JSON query in single quote on Windows, I get this error:

ERROR: too many positional options

And if I use single quotes around the ObjectId, I get this error:

Assertion: 10340:Failure parsing JSON string near: _id

So, yeah. Good luck.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.