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

标签 mongodb date mongoose aggregation-framework

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

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/

相关文章:

mysql - 将远程位置 B 到 L 的数据库中的相似数据集中到位置 A 的数据库中

javascript - mongoDB - $push _id 到特定用户

java - 直接从 Java 中的 Date 对象获取格式模式

python - 根据组的第一个日期将月份累积添加到日期列

mongodb - Mongoose 带有可选参数的复杂查询?

mysql - 联系 MySql 时 Node 连接错误?

java - CouchDb - MongoDb 和 NoSQL 数据库比较(使用 XML 文档)

php - PDO 转义斜线

node.js - mongoose.Types.ObjectId() 绝对是 Node.js 客户端操作吗?

node.js - Mongoose 对象文字子文档不可访问