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

前端 未结 8 1242
情歌与酒
情歌与酒 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 10:50

    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'
          }
        }
      }
    ])
    
    0 讨论(0)
  • 2020-11-28 10:52

    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.

    0 讨论(0)
  • 2020-11-28 10:55

    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.

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

    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.

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

    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.

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

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