如何在MongoDB中使用Loopback 3远程方法找到不同文档中日期的差异



假设我的2个文档在mongodb中是这样的。如何找到字段之间的日期差";eventCreatedAt";。我使用环回3远程方法实现。

[
{
"eventType": "offer",
"currentPrice": 3,
"seller": "Medicalstudent",
"buyer": "musiciann",
"eventCreatedAt": "2022-03-09T10:25:20.308Z",
"isExistingOffer": true,
"timeDiffOfPreTran": 1,
"id": "6228853556f56afb3d995d50",
"nftId": "622880fc56f56afb3d995d4f"
},
{
"eventType": "offer",
"currentPrice": 5,
"seller": "Medicalstudentss",
"buyer": "music",
"eventCreatedAt": "2022-03-09T10:25:20.308Z",
"isExistingOffer": false,
"timeDiffOfPreTran": 4,
"id": "622f2d8e550a568093b54c93",
"nftId": "622880fc56f56afb3d995d4f"
}
]
db.collection.aggregate([
{
$lookup: {
from: "nft",
localField: "nftId",
foreignField: "nftId",
as: "docs",
pipeline: [
{
$setWindowFields: {
partitionBy: "",
sortBy: { eventCreatedAt: 1 },
output: {
shift: {
$shift: {
output: "$eventCreatedAt",
by: -1,
default: "$$REMOVE"
}
}
}
}
},
{
$set: {
shift: {
$dateDiff: {
startDate: { $toDate: "$shift" },
endDate: { $toDate: "$eventCreatedAt" },
unit: "day"
}
}
}
}
]
}
}
])

mongoplayground


db.collection.aggregate([
{
$setWindowFields: {
partitionBy: "",
sortBy: { eventCreatedAt: 1 },
output: {
shift: {
$shift: {
output: "$eventCreatedAt",
by: -1,
default: "$$REMOVE"
}
}
}
}
},
{
$set: {
shift: {
$dateDiff: {
startDate: { $toDate: "$shift" },
endDate: { $toDate: "$eventCreatedAt" },
unit: "day"
}
}
}
}
])

mongoplayground

相关内容

  • 没有找到相关文章

最新更新