Mongoose Date - porównanie bez czasu i Group by createdAt i staffId z tygodniową, miesięczną i roczną łączną liczbą pracowników według agregacji?

Dec 14 2020

Może być pytanie, jak wygląda duplikat, ale przeproś za to.

Chcę zagregować tygodniową, miesięczną, roczną podstawę wyników createdAtnieruchomości bez czasu i staffId.

Model jest jak poniżej:

   {
    "_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
}

Oczekiwany wynik: Przykład 1) Co tydzień

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


]

Przykład 2) Co miesiąc

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

]

podobnie co rok ...

Używam nodejs i mongoose do implementacji API .

Dużo walczę, ale nie jestem w stanie osiągnąć oczekiwanego rezultatu.

jeśli ktoś mi pomoże, będzie to wielka pomoc.

Podziękowania dla wszystkich Expert.

Próbowałem czegoś takiego:

[
        {
          $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 } }
      ];

Odpowiedzi

3 turivishal Dec 14 2020 at 15:47

Możesz spróbować,

  • Grupuj według tygodnia
db.collection.aggregate([
  {
    $group: { _id: { year: { $year: "$createdAt" }, week: { $week: "$createdAt" } }, createdAt: { $first: "$createdAt" }, count: { $sum: 1 }
    }
  }
])

Plac zabaw

  • Grupuj według miesiąca
db.collection.aggregate([
  {
    $group: { _id: { year: { $year: "$createdAt" }, month: { $month: "$createdAt" } }, createdAt: { $first: "$createdAt" }, count: { $sum: 1 }
    }
  }
])

Plac zabaw

  • Grupuj według roku
db.collection.aggregate([
  {
    $group: { _id: { $year: "$createdAt" }, createdAt: { $first: "$createdAt" }, count: { $sum: 1 }
    }
  }
])

Plac zabaw

Możesz zmienić format daty z języka klienta za pomocą createdAtpola!