我想在生成的字段“ diff_days”中添加+1,并取该点左侧的值。
我的查询是:
db.Jobs.aggregate([
{ "$addFields": {
"timeDifference": { "$subtract": [ "$jobEndDateMilliSeconds",Date.now() ]},
}},
{ "$addFields": { "diff_days": {
"$divide": [
"$timeDifference",
1000 * 60 * 60 * 24
]
}
},
},
{"$match":{ jobEndDateMilliSeconds: { $gte: Date.now() }}},
{"$sort":{"timeDifference":1}},
{"$limit":1}
])
输出:
"jobProfile" : "Java Developer",
"jobID" : "1",
"jobCountry" : "India",
"openingsCount" : 10,
"jobType" : "Fulltime",
"jobDescription" : "profile description",
"postingDate" : "07-08-2018 04:21:22",
"jobStatus" : "Active",
"jobEndDate" : ISODate("2018-08-14T23:59:59.999Z"),
"jobEndDateMilliSeconds" : 1534204859999.0,
"timeDifference" : 216862093.0,
"diff_days" : 3
请帮助我。谢谢
答案 0 :(得分:1)
您可以将$add与两个参数一起使用:第一个是您的求值,第二个将1
作为常量值,然后$trunc截断为整数:
db.Jobs.aggregate([
{ "$addFields": {
"timeDifference": { "$subtract": [ "$jobEndDateMilliSeconds",Date.now() ]},
}},
{ "$addFields": { "diff_days": {
$trunc: { $add: [
{
"$divide": [
"$timeDifference",
1000 * 60 * 60 * 24
]
}, 1
] }
}
},
},
{"$match":{ jobEndDateMilliSeconds: { $gte: Date.now() }}},
{"$sort":{"timeDifference":1}},
{"$limit":1}
])