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

前端 未结 8 1268
情歌与酒
情歌与酒 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: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.

提交回复
热议问题