Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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: MongoDB: can't convert from BSON type EOO to Date.

But it doesn't tell me how to get things done.

like image 733
jsbisht Avatar asked Feb 09 '15 17:02

jsbisht


People also ask

What is BSON date?

Date. BSON Date is a 64-bit integer that represents the number of milliseconds since the Unix epoch (Jan 1, 1970). This results in a representable date range of about 290 million years into the past and future. The official BSON specification refers to the BSON Date type as the UTC datetime.

What is BSON type?

It is a 64-bit integer which represents the number of milliseconds. BSON data type generally supports UTC datetime and it is signed. If the value of the date data type is negative then it represents the dates before 1970.

Why does MongoDB use BSON?

Unlike systems that simply store JSON as string-encoded values, or binary-encoded blobs, MongoDB uses BSON to offer the industry's most powerful indexing and querying features on top of the web's most usable data format.

How does MongoDB write dates?

You can specify a particular date by passing an ISO-8601 date string with a year within the inclusive range 0 through 9999 to the new Date() constructor or the ISODate() function. These functions accept the following formats: new Date("<YYYY-mm-dd>") returns the ISODate with the specified date.


2 Answers

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.

like image 38
Anurag Pandey Avatar answered Nov 11 '22 04:11

Anurag Pandey


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.

You can find those docs with a $not query that uses the $type operator like:

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:

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); }) 
like image 120
JohnnyHK Avatar answered Nov 11 '22 03:11

JohnnyHK