mongoose 시간이없는 날짜와 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},

]

유사하게 매년 ...

저는 nodejsmongoose 를 사용하여 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필드를 사용하여 클라이언트 측 언어에서 날짜 형식을 변경할 수 있습니다 !