skip to Main Content

I’ve been working on agregation in the past days but I could not figure out how to filter documents with the same dates.

Here are the documents. At the moment, there are 5 of them.

Index 0 and index 1 has the same date and Index 2 and index 3 has the same date

The idea is, filter those documents with the same date and total the start and end on each filtered objects

Here is the full exported documents from the mongodb

 [
  {
    _id: "63df0b216557e49cd48d0399",
    start: "2023-02-05T01:49:21.677Z",
    isWorking: false,
    createdAt: "2023-02-05T01:49:21.789Z",
    updatedAt: "2023-02-05T01:49:28.109Z",
    end: "2023-02-05T01:49:28.010Z",
  },
  {
    _id: "63df0ad16557e49cd48d0390",
    start: "2023-02-05T01:48:01.843Z",
    isWorking: false,
    createdAt: "2023-02-05T01:48:01.936Z",
    updatedAt: "2023-02-05T01:48:06.355Z",
    end: "2023-02-05T01:48:06.272Z",
  },
  {
    _id: "63df0d1d47e5e303ef3f1987",
    start: "2023-02-04T01:57:08.230Z",
    isWorking: false,
    createdAt: "2023-02-04T01:57:08.230Z",
    updatedAt: "2023-02-04T01:57:08.230Z",
    end: "2023-02-04T01:57:42.900Z",
  },
  {
    _id: "63df0cc247e5e303ef3f1986",
    start: "2023-02-04T01:55:15.091Z",
    isWorking: false,
    createdAt: "2023-02-04T01:55:15.091Z",
    updatedAt: "2023-02-04T01:55:15.091Z",
    end: "2023-02-04T01:56:12.035Z",
  },
  {
    _id: "63df0d5147e5e303ef3f1988",
    start: "2023-02-03T01:58:18.018Z",
    isWorking: false,
    createdAt: "2023-02-03T01:58:18.018Z",
    updatedAt: "2023-02-03T01:58:18.018Z",
    end: "2023-02-03T01:58:35.941Z",
  },
];

2

Answers


  1. Chosen as BEST ANSWER

    After reading the documentation and watching a lot of tutorials, I think I solved it but I won't mark this as an answer yet because I know there will be some edits for this solution.

    const entries = await entryModel.aggregate( [
    {
      $addFields: {
        day: {
          $dayOfMonth: '$createdAt'
        },
        calcStartAndEnd: {
          $dateDiff: {
            startDate: '$start',
            endDate: '$end',
            unit: 'millisecond'
          }
        }
      }
    },
    {
      $group: {
        _id: '$day',
        numOfDocs: { $sum: 1 },
        docs: {
          $push: {
            _id: '$_id',
            start: '$start',
            end: '$end',
            total: '$calcStartAndEnd',
            createdAt: '$createdAt'
          }
        },
      }
    },
    ] )

    This is the result.

    [
        {
          "_id": 3,
          "numOfDocs": 1,
          "docs": [
            {
              "_id": "63df0d5147e5e303ef3f1988",
              "start": "2023-02-03T01:58:18.018Z",
              "end": "2023-02-03T01:58:35.941Z",
              "total": 17923,
              "createdAt": "2023-02-03T01:58:18.018Z"
            }
          ]
        },
        {
          "_id": 6,
          "numOfDocs": 1,
          "docs": [
            {
              "_id": "63e0fc4e7b2f0f3faf7cc9f2",
              "start": "2023-02-06T13:10:38.501Z",
              "total": null,
              "createdAt": "2023-02-06T13:10:38.555Z"
            }
          ]
        },
        {
          "_id": 4,
          "numOfDocs": 2,
          "docs": [
            {
                "_id": "63df0cc247e5e303ef3f1986",
                "start": "2023-02-04T01:55:15.091Z",
                "end": "2023-02-04T01:56:12.035Z",
                "total": 56944,
                "createdAt": "2023-02-04T01:55:15.091Z"
            },
            {
                "_id": "63df0d1d47e5e303ef3f1987",
                "start": "2023-02-04T01:57:08.230Z",
                "end": "2023-02-04T01:57:42.900Z",
                "total": 34670,
                "createdAt": "2023-02-04T01:57:08.230Z"
            }
          ]
        },
      {
        "_id": 5,
        "numOfDocs": 2,
        "docs": [
          {
              "_id": "63df0ad16557e49cd48d0390",
              "start": "2023-02-05T01:48:01.843Z",
              "end": "2023-02-05T01:48:06.272Z",
              "total": 4429,
              "createdAt": "2023-02-05T01:48:01.936Z"
          },
          {
              "_id": "63df0b216557e49cd48d0399",
              "start": "2023-02-05T01:49:21.677Z",
              "end": "2023-02-05T01:49:28.010Z",
              "total": 6333,
              "createdAt": "2023-02-05T01:49:21.789Z"
          }
        ]
      }
    ]


  2. You can use Set to avoid duplicate datas.
    Get additional information about Sets from here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search