MongoDB:将每小时数据汇总到每日聚集

发布于 2025-01-28 06:18:42 字数 2377 浏览 1 评论 0原文

我是MongoDB的新手,无法找到解决我的问题的解决方案。

我正在收集每小时的加密数据。每个文档都是对象的数组。在每个对象中,都有另一个嵌套对象阵列。看起来如下:

timestamp: "2022-05-11T12:38:01.537Z",
positions: [
    {
        detail: 1,
        name: "name",
        importantNumber: 0,
        arrayOfTokens: [
            {
                tokenName: "name",
                tokenSymbol: "symbol",
                tokenPrice: 1,
                tokensEarned: 10,
                baseAssetValueOfTokensEarned: 10,
            },
            {
                tokenName: "name2",
                tokenSymbol: "symbol2",
                tokenPrice: 2,
                tokensEarned: 10,
                baseAssetValueOfTokensEarned: 20,
            },
        ],
    },
],};

我的目标是能够将每小时的数据汇总成每日组,在该组中,时间戳成为一天的日期,位置阵列仍然包含每个位置的主要细节,总结了重要的number(我相信我一直在能够实现),并将每个小时的令牌详细信息汇总到每个令牌的一个对象中,计算平均令牌价格,赚取的总代币等。

到目前为止,我到目前为止的内容是:

    const res = await Name.aggregate([
        {
            $unwind: {
                path: "$positions",
            },
        },
        {
            $project: {
                _id: 0,
                timestamp: "$timestamp",
                detail: "$positions.detail",
                name: "$positions.name",
                importantNumber: "$positions.importantNumber",
                arrayOfTokens: "$positions.arrayOfTokens ",
            },
        },
        {
            $group: {
                _id: {
                    date: { $dateToString: { format: "%Y-%m-%d", date: "$timestamp" } },
                    name: "$name",
                },
                importantNumber: { $sum: "$importantNumber" },
                arrayOfTokens: { $push: "$arrayOfTokens" }, // it is here that I am stuck
            },
        },
    ]);

    return res;
};

记录两个小时,以下查询返回以下结果,以下结果,以下结果,使用Arrayoftokens包含多个阵列:

{
  _id: {
    date: '2022-05-11',
    name: 'name',
  },
  importantNumber: //sum of important number,
  arrayOfTokens: [
    [ [Object], [Object] ], // hour 1: token1, token2.
    [ [Object], [Object] ] // hour 2: token1, token2
  ]
}

我希望Arrayoftokens仅容纳每个令牌对象的一个​​实例。类似于以下内容:

...
arrayOfTokens: [
    {allToken1}, {allToken2} // with the name and symbol preserved, and average of the token price, sum of tokens earned and sum of base asset value.
]

任何帮助将不胜感激,谢谢。

I am new to MongoDB and have not been able to find a solution to my problem.

I am collecting hourly crypto data. Each document is an array of objects. Within each of these objects there is another nested array of objects. It looks as follows:

timestamp: "2022-05-11T12:38:01.537Z",
positions: [
    {
        detail: 1,
        name: "name",
        importantNumber: 0,
        arrayOfTokens: [
            {
                tokenName: "name",
                tokenSymbol: "symbol",
                tokenPrice: 1,
                tokensEarned: 10,
                baseAssetValueOfTokensEarned: 10,
            },
            {
                tokenName: "name2",
                tokenSymbol: "symbol2",
                tokenPrice: 2,
                tokensEarned: 10,
                baseAssetValueOfTokensEarned: 20,
            },
        ],
    },
],};

My goal is to be able to aggregate the hourly data into daily groups, where the timestamp becomes the day's date, the position array still houses the primary details of each position, sums the importantNumber (these I believe I have been able to achieve), and aggregates each hour's token details into one object for each token, calculating the average token price, the total tokens earned etc.

What I have so far is:

    const res = await Name.aggregate([
        {
            $unwind: {
                path: "$positions",
            },
        },
        {
            $project: {
                _id: 0,
                timestamp: "$timestamp",
                detail: "$positions.detail",
                name: "$positions.name",
                importantNumber: "$positions.importantNumber",
                arrayOfTokens: "$positions.arrayOfTokens ",
            },
        },
        {
            $group: {
                _id: {
                    date: { $dateToString: { format: "%Y-%m-%d", date: "$timestamp" } },
                    name: "$name",
                },
                importantNumber: { $sum: "$importantNumber" },
                arrayOfTokens: { $push: "$arrayOfTokens" }, // it is here that I am stuck
            },
        },
    ]);

    return res;
};

With two hours recorded, the above query returns the following result, with the arrayOfTokens housing multiple arrays:

{
  _id: {
    date: '2022-05-11',
    name: 'name',
  },
  importantNumber: //sum of important number,
  arrayOfTokens: [
    [ [Object], [Object] ], // hour 1: token1, token2.
    [ [Object], [Object] ] // hour 2: token1, token2
  ]
}

I would like the arrayOfTokens to house only one instance of each token object. Something similar to the following:

...
arrayOfTokens: [
    {allToken1}, {allToken2} // with the name and symbol preserved, and average of the token price, sum of tokens earned and sum of base asset value.
]

Any help would be greatly appreciated, thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑凤梨 2025-02-04 06:18:42

应该是这个:

db.collection.aggregate([
  { $unwind: { path: "$positions" } },
  {
    $group: {
      _id: {
        date: { $dateTrunc: { date: "$timestamp", unit: "day" } },
        name: "$positions.name"
      },
      importantNumber: { $sum: "$positions.importantNumber" },
      arrayOfTokens: { $push: "$positions.arrayOfTokens" }
    }
  }
])

我更喜欢$ dateTrunc而不是字符串。

mongo Playground

Should be this one:

db.collection.aggregate([
  { $unwind: { path: "$positions" } },
  {
    $group: {
      _id: {
        date: { $dateTrunc: { date: "$timestamp", unit: "day" } },
        name: "$positions.name"
      },
      importantNumber: { $sum: "$positions.importantNumber" },
      arrayOfTokens: { $push: "$positions.arrayOfTokens" }
    }
  }
])

I prefer $dateTrunc over group by string.

Mongo Playground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文