MongoDB: can't convert from BSON type EOO to Date

匿名 (未验证) 提交于 2019-12-03 08:36:05

问题:

I' trying to use aggregation framework (with ruby) and project the date like this:

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

the document is like this one:

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

but i get the following error:

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

This is strange because it works correctly if I run this on the exactly same db which is imported with mongorestore.

回答1:

The problem was that I was saving some documents without the timestamp field.



回答2:

If you needed to have some documents without this timestamp field, you could try this (I'm using Javascript/Mongoose notation):

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

In this case, every document without the timestamp field would return -1. All the other documents would return the year as expected.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!