![](/img/trans.png)
[英]How can I update the expires property on createdAt field in mongoose to avoid the document being deleted?
[英]how can i add document values depending on day createdAt in MongoDB?
我需要在 createdAt 是當前日期的每個文檔的“value”屬性中添加一個值,該文檔具有“finished”屬性為 true,並且該文檔具有“value”和“period.check_out”財產。
到目前為止,我已經能夠 select 所有具有“period.check_out”屬性的文檔並對值求和。 問題是選擇當天的文件。 我已經嘗試以多種方式按天排序,但我沒有得到。
我啟用了 nas 數據庫模型,所以每當我創建一個新文檔時,它會自動為我添加一個 createdAt、updatedAt 和 _v。
文檔示例:
[
{
"_id": "1",
"client": {
"name": "raul germano"
},
"parking": {
"_id": "5d8bfe395e0a822b143fd68d",
"value": 2.59,
"name": "pro parking oficial"
},
"finished": false,
"createdAt": "2019-09-25T23:54:32.802Z",
"updatedAt": "2019-09-25T23:54:32.802Z"
},
{
"_id": "2",
"client": {
"name": "raul vitor"
},
"parking": {
"_id": "5d8bfe395e0a822b143fd68d",
"value": 2.32,
"name": "pro parking oficial"
},
"period": {
"check_in": {
"user": "caboco",
"moment": "2019-09-25T00:05:36.273Z"
},
"check_out": {
"user": "outro caboco",
"moment": "2019-09-25T00:09:56.506Z"
}
},
"hours": 0.07,
"value": 0.17,
"finished": true,
"createdAt": "2019-09-26T23:54:33.463Z",
"updatedAt": "2019-09-26T23:54:33.463Z"
},
{
"_id": "3",
"client": {
"name": "raul germano"
},
"parking": {
"_id": "5d8bfe395e0a822b143fd68d",
"value": 2.60,
"name": "pro parking oficial"
},
"finished": true,
"createdAt": "2019-09-25T23:54:33.463Z",
"updatedAt": "2019-09-25T23:54:33.463Z",
"period": {
"check_in": {
"user": "caboco",
"moment": "2019-09-25T00:05:36.273Z"
},
"check_out": {
"user": "outro caboco",
"moment": "2019-09-25T00:09:56.506Z"
}
},
"hours": 0.09,
"value": 0.23
}
]
當前查詢:
collection.aggregate([
{
$match: {
'period.check_out': {
$exists: true
},
createdAt: {
$gte: new Date("2019-09-24")
$lt: new Date("2019-09-25")
}
}
},
{
$unwind: '$parking'
},
{
$match: {
'parking._id': Types.ObjectId(parking_id)
}
},
{
$group: {
_id: Types.ObjectId(parking_id),
total: {
$sum: '$value'
}
}
}
])
這樣他就選擇了過去的 24 小時
根據上面介紹的文檔,我只需要選擇 _id "1" 和 "2" 文檔。 從而產生
考慮到“2019-09-26”是當前日期,請嘗試將您的 $match 替換為以下內容:
{
$match: {
'period.check_out': {
$exists: true
},
createdAt: {
$gte: new Date('2019-09-26'),
$lte: new Date(),
}
}
},
你匹配的是:$gte: 2019-09-24T00:00:00.000Z, $lte: 2019-09-25T00:00:00.000Z。 而且我認為這不會是當前日期。 相反,您需要匹配的是 '2019-09-24T00:00:00.000Z' - 當前時間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.