mongodb 异常:无法从 BSON 类型 EOO 转换为日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28415995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
exception: can't convert from BSON type EOO to Date
提问by jsbisht
I am getting an issue for running the following aggregate query:
我在运行以下聚合查询时遇到问题:
db.snippets.aggregate([ { '$project': { month: { '$month': '$created_at' }} } ])
The error message for the same is:
相同的错误消息是:
assert: command failed: {
"errmsg" : "exception: can't convert from BSON type EOO to Date",
"code" : 16006,
"ok" : 0 } : aggregate failed
How do i get around this issue. I found a related question
我如何解决这个问题。我发现了一个相关的问题
Related Stack Overflow question
But it doesnt tell how to get things done.
但它没有说明如何完成任务。
回答by JohnnyHK
You likely have one or more docs with a created_at
value that's not a BSON Date
and you'll need to fix that by converting those values to Date
or removing them.
您可能有一个或多个文档的created_at
值不是 BSON Date
,您需要通过将这些值转换为Date
或删除它们来解决该问题。
You can find those docs with a $not
query that uses the $type
operator like:
您可以$not
通过使用以下$type
运算符的查询找到这些文档:
db.snippets.find({created_at: {$not: {$type: 9}}})
If the created_at
values are date strings, you can find the docs that need updating and then update them in the shell using code like:
如果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);
})
回答by FRocha
In some situations, some documents are supposed to have empty Date fields. In those cases, you could try this (using your example):
在某些情况下,某些文档应该具有空的日期字段。在这些情况下,你可以试试这个(使用你的例子):
db.snippets.aggregate([ { '$project': { month:
{ $cond: [{ $ifNull: ['$created_at', 0] }, { $month: '$created_at' }, -1] }} } ])
In this example, we would get -1 in the cases whenever no field '$created_at' is found. For all the other cases, we would get the Date month.
在这个例子中,在没有找到字段 '$created_at' 的情况下,我们将得到 -1。对于所有其他情况,我们将获得日期月份。
回答by Skipwave
I had a related issue, but in my case the Date fields were the members of an array, so the error was "can't convert BSON type Object to Date".
我有一个相关的问题,但就我而言,日期字段是数组的成员,因此错误是“无法将 BSON 类型对象转换为日期”。
I needed to get the day of week from the dates in the possibleTripDateTimes array.
我需要从 possibleTripDateTimes 数组中的日期获取星期几。
Sample document:
示例文件:
{
"possibleTripDateTimes" : [
{
"tripDateTime" : ISODate("2015-08-01T06:00:00.000-0700")
}
]
}
The fix was simply to use dot notation to address the array member fields.
修复只是使用点符号来解决数组成员字段。
db.trips.aggregate([
{
$project: {
departTime: {
$map: {
input: "$possibleTripDateTimes.tripDateTime",
as: "dateTime",
in: { $dayOfWeek: "$$dateTime" }
}
}
}
}
]
);
I hope this helps someone who also gets zero search results on the "BSON type Object" search
我希望这可以帮助那些在“BSON 类型对象”搜索中也获得零搜索结果的人
回答by facumedica
I had a similar problem, and solved it checking if the date existed.
我有一个类似的问题,并通过检查日期是否存在来解决它。
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}}
])
My date field is bd
and with that match I'm getting all users that have their birthday on January 15th.
我的日期字段是,bd
并且通过该匹配,我得到了所有生日在 1 月 15 日的用户。
回答by Wolf7176
I had the same problem, I figured that the date field is missing for some of the documents causing the conversion to fail. I just added a match clause to filter these out. But ofcourse i am investigating on my app side why they are not being populated.
我遇到了同样的问题,我认为某些导致转换失败的文档缺少日期字段。我只是添加了一个匹配子句来过滤掉这些。但是,当然我正在我的应用程序方面调查为什么它们没有被填充。
db.snippets.aggregate([
{
'$match': {
'created_at': {
"$exists": true
}
}
},
{
'$project': {
month: {
'$month': '$created_at'
}
}
}
])
回答by Anurag Pandey
try this one, its help for me above problem.
试试这个,它对我上面的问题有帮助。
db.snippets.aggregate([{
'$project': {
month: { $substr: ["$created_at", 5, 2] }
}
}]);
above code get month wise
上面的代码得到明智的月份
data is entered into the database in ISO format which can then be easily worked with.
数据以 ISO 格式输入数据库,然后可以轻松使用。
回答by Enda Molloy
This error can also appear if you have incorrectly named your properties in your aggregation relative to what they are in your database.
如果您在聚合中错误地命名了与数据库中的属性相关的属性,也会出现此错误。
For example my code was
例如我的代码是
$group: {
_id: {$week: "$projects.timeStamp"},
total: { $sum: "$projects.hours" }
}
But I hadn't camelcased timestamp in my database so simply modifying to projects.timestamp
fixed it.
但是我没有在我的数据库中使用驼峰式时间戳,所以只需修改即可projects.timestamp
修复它。
回答by ABDUL JAMAL
First, you need to check whether the data type is in ISODate. IF not you can change the data type as the following example.
首先,您需要检查数据类型是否在 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);})
Now you can find it in two ways
现在你可以通过两种方式找到它
db.collectionName.find({ $expr: {$eq: [{ $year: "$your_date_field" }, 2017]}});
Or by aggregation
或者通过聚合
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}}])