Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 例外情况:can';t从BSON类型EOO转换为日期_Mongodb_Date_Mongoose_Aggregation Framework - Fatal编程技术网

Mongodb 例外情况:can';t从BSON类型EOO转换为日期

Mongodb 例外情况:can';t从BSON类型EOO转换为日期,mongodb,date,mongoose,aggregation-framework,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&qu

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

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
我如何回避这个问题?我发现了一个相关的问题:


但是它并不能告诉你如何完成任务。

你可能有一个或多个文档的
创建值不是BSON
Date
,你需要通过将这些值转换为
Date
或删除它们来修复

您可以找到带有
$not
查询且使用以下运算符的文档:

db.snippets.find({created_at:{$not:{$type:9}})
如果在
处创建的
值是日期字符串,您可以找到需要更新的文档,然后在shell中使用如下代码进行更新:

db.snippets.find({created_at:{$not:{$type:9}}}).forEach(函数(doc){
//将在处创建的_转换为日期
创建日期=新日期(创建日期);
db.snippets.save(doc);
})

在某些情况下,某些文档应该有空的日期字段。在这些情况下,您可以尝试以下方法(使用您的示例):


在本例中,只要没有找到字段“$created_at”,我们就会得到-1。对于所有其他情况,我们都会得到日期月份。

试试这个,它对我解决上述问题有帮助

db.snippets.aggregate([{
'$project': {
    month: { $substr: ["$created_at", 5, 2] }
}
 }]);
上面的代码将按月显示


数据以ISO格式输入数据库,然后可以轻松使用

我遇到了一个相关问题,但在我的例子中,日期字段是数组的成员,因此错误是“无法将BSON类型对象转换为日期”

我需要从possibleTripDateTimes数组中的日期获取星期几

样本文件:

{
"possibleTripDateTimes" : [
    {
        "tripDateTime" : ISODate("2015-08-01T06:00:00.000-0700")
    }
]
}
修复方法只是使用点表示法来处理数组成员字段

db.trips.aggregate([
  {
       $project: {
         departTime: {
           $map: {
             input: "$possibleTripDateTimes.tripDateTime",
             as: "dateTime",
             in: { $dayOfWeek: "$$dateTime" }
           }
   }
  }
}
]
);

我希望这能帮助那些在“BSON-type-Object”搜索中也没有搜索结果的人

我遇到了一个类似的问题,并解决了检查日期是否存在的问题

db.users.aggregate([
{$project:{day:  { $cond: ["$bd", { $dayOfMonth: "$bd" }, -1] },
           month:  { $cond: ["$bd", { $month: "$bd" }, -1] },
           year:  { $cond: ["$bd", { $year: "$bd" }, -1] }
           }},
{$match:{"month":1, "day":15}}
])

我的日期字段是
bd
,通过这一匹配,我得到了所有在1月15日过生日的用户。

我也遇到了同样的问题,我发现一些文档缺少日期字段,导致转换失败。我刚刚添加了一个match子句来过滤这些内容。但当然,我正在我的应用程序端调查为什么它们没有被填充

db.snippets.aggregate([
  {
    '$match': {
      'created_at': {
        "$exists": true
      }
    }
  },
  {
    '$project': {
      month: {
        '$month': '$created_at'
      }
    }
  }
])

如果聚合中的属性相对于数据库中的属性命名不正确,也会出现此错误

例如,我的代码是

$group: {
        _id: {$week: "$projects.timeStamp"},
        total: { $sum: "$projects.hours"  }
    }

但我没有在数据库中添加时间戳,所以只需修改
项目即可。时间戳
修复了它

首先,您需要检查数据类型是否在ISODate中。如果没有,您可以更改数据类型,如下例所示

db.collectionName.find().forEach(function(each_object_from_collection){each_object_from_collection.your_date_field=new ISODate(each_object_from_collection.your_date_field);db.collectionName.save(each_object_from_collection);})
现在你可以通过两种方式找到它

db.collectionName.find({ $expr: {$eq: [{ $year: "$your_date_field" }, 2017]}});
或者通过聚合

db.collectionName.aggregate([{$project: {field1_you_need_in_result: 1,field12_you_need_in_result: 1,your_year_variable: {$year: '$your_date_field'}, your_month_variable: {$month: '$your_date_field'}}},{$match: {your_year_variable:2017, your_month_variable: 3}}])

你让我开心。因为这个原因我被困了很长一段时间。@johnyhk这个查询不就是找到没有BSON日期的文档吗?如何查找和更新所有非BSON日期?@user137717将
.find(conditions)
推断为
.update(conditions,update)
@johnyhk谢谢。你让我很开心。如果有人在这里寻找相反的结果,例如,所有文档的日期字段都有毫秒,请看:啊!!这太酷了。你帮我省了很多愚蠢的搜索时间!谢谢男人:)
db.collectionName.aggregate([{$project: {field1_you_need_in_result: 1,field12_you_need_in_result: 1,your_year_variable: {$year: '$your_date_field'}, your_month_variable: {$month: '$your_date_field'}}},{$match: {your_year_variable:2017, your_month_variable: 3}}])