'Mongoose Aggregate Data Query

I have a collection that returns a document with historical value recordings like this:

{
    "_id" : ObjectId("616eb07b1e7edf6e9b0356d8"),
    "collectibleId" : "ae9b4b16-bc4e-4457-9c69-a3306c976ed7",
    "__v" : 0,
    "createdAt" : ISODate("2021-10-19T11:48:11.106Z"),
    "history" : [ 
        {
            "_id" : ObjectId("61b7cafa3c375e0522534034"),
            "hourlyChange" : {
                "market" : 0
            },
            "prevSold" : {
                "price" : 935,
                "createdAt" : ISODate("2021-10-19T01:40:11.187Z"),
                "issueNumber" : 206,
                "listingType" : "FIXED"
            },
            "storePrice" : 89.99,
            "issueNumber" : 2606,
            "value" : 699,
            "totalListings" : 139,
            "date" : ISODate("2021-10-19T11:48:11.105Z"),
            "marketCap" : 97161
        }, 
        ....100+ more objects...
   ]
}

I want to be able to request a certain collectibleId and to perform some calculations on this to get the percentage change between x date and y date as well as find the max and min number - all of these are calculated from the history.value field.

I know in mongodb I could use the $setWindowFields to help with this, but it's not available in mongoose. The output im looking to get is just something similar to this

    "collectibleId" : "ae9b4b16-bc4e-4457-9c69-a3306c976ed7",
    "one_day_change" : 20%,
    "one_week_change": 40%,
    "one_month_change": -50%
    "all_time_high" : 100,
    "all_time_low" : 1

What I have so far is this but it's not quite working and returning null.

[{
    $match: {
        collectibleId: "ae9b4b16-bc4e-4457-9c69-a3306c976ed7"
    }
}, {
    $group: {
        _id: "$history",
    }
}, {
    $set: {
        "target-date": "$$NOW"
    }
}, {
    $facet: {
        "one-day": [{
                "$match": {
                    "$expr": {
                        "$lte": [{
                                "$subtract": ["$target-date", "$date"]
                            },
                            {
                                "$multiply": [24, 60, 60, 1000]
                            }
                        ]
                    }
                }
            },
            {
                "$group": {
                    "_id": null,
                    "max-price": {
                        "$max": "$value"
                    },
                    "min-price": {
                        "$min": "$value"
                    }
                }
            },
            {
                "$unset": ["_id"]
            }
        ],
"one-week":
                        [{
                            "$match":
                                {
                                    "$expr":
                                        {
                                            "$lte":
                                                [{"$subtract": ["$target-date", "$date"]},
                                                    {"$multiply": [7, 24, 60, 60, 1000]}]
                                        }
                                }
                        },
                            {
                                "$group":
                                    {
                                        "_id": null,
                                        "max-price": {"$max": "$value"},
                                        "min-price": {"$min": "$value"}
                                    }
                            },
                            {"$unset": ["_id"]}]
    }
}]

Thank you



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source