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

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

问题描述

尝试对 mongoexport 使用查询会导致错误.但是同样的查询被 mongo-client 评估没有错误.

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

在 mongo 客户端:

In mongo-client:

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

使用 mongoexport:

with mongoexport:

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

产生的错误:

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: [ {

但是在 mongoexport 中预先在 Date 中做乘法:

But doing the multiplication in Date beforehand in mongoexport:

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

有效!

为什么 mongo 在这两种情况下对查询的评估不同?

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

推荐答案

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

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

JSON 格式最初源自 JavaScript 的对象表示法,但无需在 JavaScript 解释器中eval()即可解析 JSON 文档的内容.

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.

您应该将 JSON 视为结构化数据",将 JavaScript 视为可执行代码".因此,实际上,您正在运行的查询有两种不同的上下文.

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.

mongo 命令行实用程序是一个 交互式 JavaScript shell 包括一个 JavaScript 解释器以及一些用于处理 MongoDB 的辅助函数.虽然 JavaScript 对象格式看起来类似于 JSON,但您也可以使用 JavaScript 对象、函数调用和运算符.

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.

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

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天全站免登陆