mongoexport JSON解析错误 [英] mongoexport JSON parsing error

查看:1109
本文介绍了mongoexport JSON解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试对mongoexport使用查询会导致错误。



在mongo-client中:

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

与mongoexport:

  mongoexport -d事件-c上市-q{created_at:新日期(1221029382 * 1000)}'

生成的错误:

 周五11月11日17时44分08秒断言:10340:故障解析JSON字符串附近:
$和:[{
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(主+ 0x32)0xa92282]
/lib/libc.so.6( __libc_start_main + 0xF4中)[0x7fbd056a61c4]
mongoexport(__ gxx_personality_v0 + 0x3d9)0x4fca29]
断言:10340故障解析JSON字符串附近:$和:[{

但在mongoexport中事先在 Date 中执行乘法:

  mongoexport -d event -c listing -q'{created_at:new Date(1221029382000)}'
$ b pre>
$ b

$ b

解决方案

mongoexport 命令行实用工具支持通过一个查询中的JSON 格式,但您尝试评估 JavaScript



JSON格式最初源于JavaScript的对象符号, JSON文档可以在JavaScript解释器中无需 eval()解析。



结构化数据和JavaScript作为可执行代码。因此,对于您运行的查询事实上有两种不同的上下文。



命令行中的 mongo 实用程序是一个交互式JavaScript外壳,其中包括JavaScript解释器以及一些用于处理MongoDB的帮助函数。虽然JavaScript对象格式与JSON类似,但您也可以使用JavaScript对象,函数调用和运算符。



您的示例 1221029382 * 1000 是由JavaScript解释器执行的数学运算的结果,如果你在 mongo shell中运行它;在JSON中,它是一个新的日期无效的值,因此mongoexport退出与失败解析JSON字符串错误。


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?

解决方案

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.

这篇关于mongoexport JSON解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆