运行以下聚合查询时遇到问题:
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: can't convert from BSON type EOO to Date .
但它并没有告诉我如何完成工作。
最佳答案
您可能有一个或多个文档的 created_at
值不是 BSON Date
,您需要通过将这些值转换为 Date 来解决这个问题
或删除它们。
您可以通过 $not
查询找到这些文档,该查询使用 $type
像这样的运算符:
db.snippets.find({created_at: {$not: {$type: 9}}})
如果 created_at
值是日期字符串,您可以找到需要更新的文档,然后使用以下代码在 shell 中更新它们:
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);
})
关于mongodb - 异常 : can't convert from BSON type EOO to Date,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28415995/