【问题标题】:how can i add document values depending on day createdAt in MongoDB?如何根据 MongoDB 中的创建日期添加文档值?
【发布时间】:2023-04-10 17:46:01
【问题描述】:

我需要在每个文档的“值”属性中添加一个值,该值是 createdAt 是当前日期,该文档具有“已完成”属性为 true,并且该文档具有“值”和“期间” .check_out" 属性。

到目前为止,我已经能够选择所有具有“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" 文档。从而产生

【问题讨论】:

    标签: javascript node.js mongodb express mongoose


    【解决方案1】:

    考虑到“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' - 当前时间。

    【讨论】:

    • 但是“new Date('2019-09-26')”又回来了“Wed Sep 25 2019 21:00:00 GMT-0300 (Horário Padrãode Brasília)”
    • 好的。然后,您需要设法找到当天的第一秒,即“2019-09-26T00:00:00.000Z”。
    猜你喜欢
    • 2013-05-08
    • 2020-12-13
    • 2014-02-13
    • 1970-01-01
    • 2020-05-16
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多