问题描述
我正在运行以下聚合查询问题:
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
我如何解决这个问题。我发现一个相关问题
How do i get around this issue. I found a related question
但它并没有告诉如何完成任务。
But it doesnt tell 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.
您可以找到使用运算符,如:
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
值是日期字符串,您可以找到需要更新然后更新的文档em在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转换为Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!