例外:无法从 BSON 类型 EOO 转换为日期 [英] Exception: can't convert from BSON type EOO to Date
问题描述
我在运行以下聚合查询时遇到问题:
I am getting an issue for running the following aggregate query:
db.snippets.aggregate([ { '$project': { month: { '$month': '$created_at' }} } ])
相同的错误信息是:
assert: command failed: {
"errmsg" : "exception: can't convert from BSON type EOO to Date",
"code" : 16006,
"ok" : 0 } : aggregate failed
我该如何解决这个问题?我发现了一个相关的问题:MongoDB:无法从 BSON 转换输入 EOO 到日期.
How do I get around this issue? I found a related question: MongoDB: can't convert from BSON type EOO to Date.
但它没有告诉我如何完成工作.
But it doesn't tell me how to get things done.
推荐答案
您可能有一个或多个文档的 created_at
值不是 BSON Date
并且您'需要通过将这些值转换为 Date
或删除它们来解决这个问题.
You likely have one or more docs with a created_at
value that's not a BSON Date
and you'll need to fix that by converting those values to Date
or removing them.
您可以通过使用 $type
运算符如:
You can find those docs with a $not
query that uses the $type
operator like:
db.snippets.find({created_at: {$not: {$type: 9}}})
如果 created_at
值是日期字符串,您可以找到需要更新的文档,然后使用如下代码在 shell 中更新它们:
If the created_at
values are date strings, you can find the docs that need updating and then update them in the shell using code like:
db.snippets.find({created_at: {$not: {$type: 9}}}).forEach(function(doc) {
// Convert created_at to a Date
doc.created_at = new Date(doc.created_at);
db.snippets.save(doc);
})
这篇关于例外:无法从 BSON 类型 EOO 转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!