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

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);
})

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.


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.


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".

I needed to get the day of week from the dates in the possibleTripDateTimes array.

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