mongoexport JSON解析错误
尝试将查询与 mongoexport 一起使用会导致错误。但 mongo-client 评估相同的查询时不会出现错误。
在 mongo-client 中:
db.listing.find({"created_at":new Date(1221029382*1000)})
使用 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
中进行乘法
mongoexport -d event -c listing -q '{"created_at":new Date(1221029382000)}'
运算!
为什么 mongo 在这两种情况下对查询的评估不同?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
mongoexport
命令行实用程序支持以 JSON 格式,但您正在尝试评估 JavaScript在你的查询中。JSON 格式最初源自 JavaScript 的对象表示法,但无需在 JavaScript 解释器中使用
eval()
即可解析 JSON 文档的内容。您应该将 JSON 视为表示“结构化数据”,将 JavaScript 视为“可执行代码”。因此,实际上,您正在运行的查询有两个不同的上下文。
mongo
命令行实用程序是一个交互式 JavaScript shell其中包括 JavaScript 解释器以及一些用于使用 MongoDB 的辅助函数。虽然 JavaScript 对象格式看起来与 JSON 类似,但您也可以使用 JavaScript 对象、函数调用和运算符。您的
1221029382*1000
示例是数学运算的结果,如果您在mongo
shell 中运行该数学运算,则该数学运算将由 JavaScript 解释器执行;在 JSON 中,它是新日期的无效值,因此 mongoexport 正在退出并出现“解析 JSON 字符串失败”错误。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 themongo
shell; in JSON it's an invalid value for a new Date so mongoexport is exiting with a "Failure parsing JSON string" error.我在执行 mongoexport 时也遇到此错误,但原因不同。我将在这里分享我的解决方案,因为我在尝试解决我的问题时最终进入了这个页面。
我知道这与这个问题没什么关系,但这篇文章的标题在谷歌中提到了它,所以既然我遇到了完全相同的错误,我将添加一个答案。希望它能帮助某人。
我试图在 Windows 控制台中执行 MongoId
_id
查询。问题是我需要将 JSON 查询用双引号括起来,并且 ObjectId 也必须用双引号(而不是单引号!)。所以我必须转义 ObjectId 引号。mongoexport -u 用户名 -pPASSWORD -d 数据库 -c 集合
--query "{_id : ObjectId(\"5148894d98981be01e000011\")}"
如果我在 Windows 上将 JSON 查询用单引号括起来,则会收到此错误:
如果我在 ObjectId 周围使用单引号,则会收到此错误:
所以, 是的。祝你好运。
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:
And if I use single quotes around the ObjectId, I get this error:
So, yeah. Good luck.