英文:
how to add 7 days in date manually using mongodb(aggregation)?
问题
{
"$match": {
"publishedAt": {
"$lt": new Date(startDate),
"$gte": new Date(endDate)
}
},
"$set": {
"publishedAt": {
"$add": [new Date(startDate), 1000 * 60 * 60 * 24 * 7]
}
}
}
英文:
I have a query in which I'm getting data using start Date and end Date. And it's working perfectly fine. now I want to add 7 days in my start date using aggregation and want to get data of start Date + 7 added days. i have tried the below code but couldn't get any success
{
$match: {
publishedAt: {
$lt: new Date(startDate),
$gte: new Date(endDate),
},
},
},
{
$set: {
publishedAt: {
$add: [new Date(startDate), 1000 * 60 * 60 * 24],
},
},
},
答案1
得分: 1
{
$set: {
publishedAt: {
$dateAdd: {
startDate: new Date(startDate),
unit: "day",
amount: 7
}
}
}
}
或者使用第三方库,例如Luxon
{
$set: {
publishedAt: DateTime.now().plus({ days: 7 }).toJSDate()
}
}
英文:
Use this:
{
$set: {
publishedAt: {
$dateAdd: {
startDate: new Date(startDate),
unit: "day",
amount: 7
}
}
}
}
Or use a 3rd party library, e.g. Luxon
{
$set: {
publishedAt: DateTime.now().plus({ days: 7 }).toJSDate()
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论