【问题标题】:MongoDB: can't convert from BSON type EOO to DateMongoDB:无法从 BSON 类型 EOO 转换为 Date
【发布时间】:2013-04-18 04:14:04
【问题描述】:

我正在尝试使用聚合框架(使用 ruby​​)并像这样投影日期:

db['requests'].aggregate([
{"$project" => {
    _id: 0, 
    method: '$method', 
    user: '$user', 
    year: {'$year' => '$timestamp'}
}}])

文件是这样的:

{
_id: ObjectId("5177d7d7df26358289da7dfd"),
timestamp: ISODate("2013-04-12T03:58:05+00:00"),
method: "POST",
status: "200",
inputsize: "874",
outputsize: "4981",
user: "131"
}

但我收到以下错误:

Mongo::OperationFailure: Database command 'aggregate' failed: (errmsg: 'exception: can't convert from BSON type EOO to Date'; code: '16006'; ok: '0.0').

这很奇怪,因为如果我在使用 mongorestore 导入的完全相同的数据库上运行它,它可以正常工作。

【问题讨论】:

  • 显然传递给 aggregate() 的数组的顺序很重要。如果您有省略某些内容的字段,则需要将其添加到 $match 中,并使 $match 成为数组中的第一个元素。 IE。 > db.user_account.aggregate( [ { $match : { "uts" : { $exists : true }, "chan_key" : "333261c7a72650a95c68d30cd70" } } , { $project : { "period_month": { $month: "$uts " } } }, { $group: { _id : { "period_month" : "$period_month" }, "number" : { $sum : 1 } } } ])

标签: ruby mongodb aggregation-framework database nosql


【解决方案1】:

问题是我保存了一些没有时间戳字段的文档。

【讨论】:

  • 是的!我也是。我搜索了我的时间戳为“nil”并且只有一个文档的文档。我解决了这个问题,瞧!查询有效。
  • 你能举个例子说明你是如何找到这些文件的吗?
【解决方案2】:

如果您需要一些没有此时间戳字段的文档,您可以试试这个(我使用的是 Javascript/Mongoose 表示法):

year: { $cond: [{ $ifNull: ['$timestamp', 0] }, { $year: '$deliveryDateEnd' }, -1] }

在这种情况下,每个没有时间戳字段的文档都将返回 -1。所有其他文档将按预期返回年份。

【讨论】:

    猜你喜欢
    • 2017-04-22
    • 2015-04-09
    • 2020-11-09
    • 2014-07-16
    • 2020-10-23
    • 2017-09-23
    • 2018-05-24
    • 2017-11-04
    相关资源
    最近更新 更多