如何在mongoDB中获取日期差异?日期应该是天数的差异?

问题描述 投票:0回答:1

我想根据当前日期减去到期日期对象来过滤集合,并且小于等于10天。

enter image description here

我使用下面的代码,但我得到的日期差异为毫秒。我想要完全一天的差异。

db.metaobject.aggregate( 
    { $unwind :'$certifications.expiry_date'},
    {$project:{
            _id:1,name:1,date-Difference: { $divide:[ {$subtract: [ "$certifications.expiry_date",new Date ]},86400000] }
        }
    },
    {$match:{
          dateDifference:{$lte:10}
        }
    }

)
node.js mongodb mongoose mongodb-query
1个回答
0
投票

如果要在节点中使用它,则不需要计算差异:

直接计算节点js中的日期+10然后只计算$ lte:

var date = new Date();
var date10 = new Date(date.getTime());
date10.setDate(date10.getDate() + 10);

db.metaobject.aggregate( 
{ $unwind :'$certifications.expiry_date'},

{$match:{
      dateDifference:{$lte: new Date(date10).toJSON()}
    }
}

)
© www.soinside.com 2019 - 2024. All rights reserved.