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

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

问题描述

尝试使用带有mongoexport的查询会导致错误。但是同样的查询是由mongo-client进行评估而没有错误的。



在mongo-client中:

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

与mongoexport:

  mongoexport -d事件-c列表-q'{created_at新日期(1221029382 * 1000)}'

生成的错误:

  Fri Nov 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(main + 0x32)[0xa92282]
/lib/libc.so.6 __libc_start_main + 0xf4)[0x7fbd 056a61c4]
mongoexport(__ gxx_personality_v0 + 0x3d9)[0x4fca29]
断言:10340解析JSON字符串附近的失败:$和:[{

但是在mongoexport中预先在 Date 中进行乘法:

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

工作!



为什么mongo在这两个上下文中不同地评估查询?

解决方案

命令行实用程序 mongoexport 支持在 JSON 格式,但您正在尝试评估 JavaScript



JSON格式最初源自于JavaScript的对象符号,但是可以解析一个JSON文档的内容您可以将JSON视为代表结构化的

数据和JavaScript作为可执行代码。因此,实际上,您正在运行的查询有两种不同的上下文。



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



您的示例 1221029382 * 1000如果您在 mongo shell中运行,则会由JavaScript解释器执行的数学操作的结果;在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天全站免登陆