mongodb - 异常 : can't convert from BSON type EOO to

运行以下聚合查询时遇到问题:

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/

相关文章:

javascript - MongoDB mongoose 弃用警告

mongodb - 奇怪的 mongodb 和 mongoose 错误 : not master a

javascript - 如何在 MongoDB 中对集合记录中的数组进行排序?

mongodb - 更新 Mongodb 中的嵌入文档属性

c# - MongoDB C# Driver 2.0 - 更新文档

mongodb - 复制/克隆 mongodb 数据库及其数据

mongodb - 使用 apt-get 安装 mongodb-10gen 失败

mongodb - 如何将 mongodb 数据库转移到另一台看不到第一个的机器上

mongodb - 无法启动mongodb服务

mongodb - 在 MongoDB 中,如果删除了集合,索引也会自动删除吗?