マングースの日付を時間なしで比較し、createdAtとstaffIdでグループ化し、週次、月次、年次の合計スタッフ数を集計しますか?

Dec 14 2020

質問は重複しているように見えますが、お詫びするかもしれません。

createdAt時間のない物件の週次、月次、年次の結果ベースを集計したいstaffId

モデルは次のようになります。

   {
    "_id" : ObjectId("5f351f3d9d90b1281c44c5dp"),
    "staffId" : 12345,
    "category" : "trend",
    "page_route" : "http://example.com/rer",
    "expireAt" : ISODate("2020-08-13T11:08:45.196Z"),
    "createdAt" : ISODate("2020-08-13T11:08:45.199Z"),
    "updatedAt" : ISODate("2020-08-13T11:08:45.199Z"),
    "__v" : 0
}
{
    "_id" : ObjectId("5f351f3d9d90b1281c44c5de"),
    "staffId" : 12346,
    "category" : "incident",
    "page_route" : "http://example.com/rergfhfhf",
    "expireAt" : ISODate("2020-08-14T11:08:45.196Z"),
    "createdAt" : ISODate("2020-08-08T11:08:45.199Z"),
    "updatedAt" : ISODate("2020-08-12T11:08:45.199Z"),
    "__v" : 0
}
{
    "_id" : ObjectId("5f351f3d9d90b1281c44c5dc"),
    "staffId" : 12347,
    "category" : "trend",
    "page_route" : "http://example.com/rerrwe",
    "expireAt" : ISODate("2020-08-13T11:08:45.196Z"),
    "createdAt" : ISODate("2020-08-13T11:08:45.199Z"),
    "updatedAt" : ISODate("2020-08-13T11:08:45.199Z"),
    "__v" : 0
}
{
    "_id" : ObjectId("5f351f3d9d90b1281c44c5dr"),
    "staffId" : 12348,
    "category" : "trend",
    "page_route" : "http://example.com/rerrwe",
    "expireAt" : ISODate("2020-08-12T11:08:45.196Z"),
    "createdAt" : ISODate("2020-08-08T11:08:45.199Z"),
    "updatedAt" : ISODate("2020-08-12T11:08:45.199Z"),
    "__v" : 0
}

期待される結果:例1)毎週

[
{_id: "2020-11-13", total: 2},
{_id: "2020-11-8", total: 2},


]

例2)毎月

[
{_id: "2020-11-8", total: 4},

]

同様に毎年...

私が使用していますnodejsマングースを実装するためのAPIを

苦労していますが、期待通りの結果が得られません。

誰かが私を助けてくれれば、それは大きな助けになるでしょう。

すべてのエキスパートに感謝します。

私はこのようなことを試しました:

[
        {
          $match: { createdAt: { $gte: new Date(currentDate), $lt: new Date(nextDate) } } }, { $project: {
            _id: 1,
            staffId: 1,
            day: {
              $dayOfMonth: "$createdAt"
            },
            month: {
              $month: "$createdAt"
            },
            year: {
              $year: "$createdAt"
            }
          }
        },
        {
          $project: { _id: 1, staffId: 1, datetime: { $concat: [
                {
                  $substr: ["$year", 0, 4]
                },
                "-",
                {
                  $substr: ["$month", 0, 2]
                },
                "-",
                {
                  $substr: ["$day", 0, 2]
                }
              ]
            }
          }
        },
        {
          $group: { _id: { createdAt: "$datetime",
              staffId: "$staffId" } } }, { $group: {
            _id: {$week:"$_id.createdAt"},
            total: {
              $sum: 1 } } }, { $sort: { _id: 1 } }
      ];

回答

3 turivishal Dec 14 2020 at 15:47

あなたが試すことができます、

  • 週ごとにグループ化
db.collection.aggregate([
  {
    $group: { _id: { year: { $year: "$createdAt" }, week: { $week: "$createdAt" } }, createdAt: { $first: "$createdAt" }, count: { $sum: 1 }
    }
  }
])

遊び場

  • 月ごとにグループ化
db.collection.aggregate([
  {
    $group: { _id: { year: { $year: "$createdAt" }, month: { $month: "$createdAt" } }, createdAt: { $first: "$createdAt" }, count: { $sum: 1 }
    }
  }
])

遊び場

  • 年ごとにグループ化
db.collection.aggregate([
  {
    $group: { _id: { $year: "$createdAt" }, createdAt: { $first: "$createdAt" }, count: { $sum: 1 }
    }
  }
])

遊び場

createdAtフィールドを使用して、クライアント側の言語から日付形式を変更できます。