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

前端 未结 8 1289
情歌与酒
情歌与酒 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: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);
    })
    

提交回复
热议问题