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

前端 未结 8 1243
情歌与酒
情歌与酒 2020-11-28 10:26

I am getting an issue for running the following aggregate query:

db.snippets.aggregate([ { \'$project\': { month: { \'$month\': \'$created_at\' }} } ])


        
相关标签:
8条回答
  • 2020-11-28 11:06

    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

    0 讨论(0)
  • 2020-11-28 11:12

    First, you need to check whether the data type is in ISODate. IF not you can change the data type as the following example.

    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}}])
    
    0 讨论(0)
提交回复
热议问题