mongoose Tarihin zamansız ve Oluşturulan ve personel kimliğine göre Grubu, Haftalık, aylık ve Yıllık toplam personel sayısı ile karşılaştırması?

Dec 14 2020

Soru iki kopya gibi görünebilir ama bunun için özür dile.

createdAtMülkün haftalık, Aylık, Yıllık sonuç bazında zamansız ve staffId.

Model aşağıdaki gibidir:

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

Beklenen sonuç: Örnek 1) Haftalık

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


]

Örnek 2) Aylık

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

]

benzer şekilde yıllık ...

Ben kullanıyorum nodejs ve firavun faresi uygulamak için API .

Çok çabalıyorum ama beklenen sonuca ulaşamıyorum.

Biri bana yardım ederse, o zaman çok yardımcı olur.

Tüm Uzmanlara teşekkürler.

Bunun gibi bir şey denedim:

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

Yanıtlar

3 turivishal Dec 14 2020 at 15:47

Deneyebilirsin,

  • Haftaya göre gruplama
db.collection.aggregate([
  {
    $group: { _id: { year: { $year: "$createdAt" }, week: { $week: "$createdAt" } }, createdAt: { $first: "$createdAt" }, count: { $sum: 1 }
    }
  }
])

Oyun alanı

  • Aya göre gruplama
db.collection.aggregate([
  {
    $group: { _id: { year: { $year: "$createdAt" }, month: { $month: "$createdAt" } }, createdAt: { $first: "$createdAt" }, count: { $sum: 1 }
    }
  }
])

Oyun alanı

  • Yıla göre gruplama
db.collection.aggregate([
  {
    $group: { _id: { $year: "$createdAt" }, createdAt: { $first: "$createdAt" }, count: { $sum: 1 }
    }
  }
])

Oyun alanı

createdAtAlan kullanarak tarih formatını istemci tarafı dilinizden değiştirebilirsiniz !