Search code examples
javascriptnode.jsmongodbmongooseaggregate

nodejs mongodb aggregation not working properly


I have this type of MongoDB collection

{
  "uploaded_video": [
    {
      "_id": "622a03e66d59f72b72358069",
      "user_id": "Ec3FNhk45Lh1xk92cO9eoXDiWEk2",
      "uploaded_video": {
        "_id": "622a0616242e5b2e65e8b3b4",
        "video_description": "test",
        "created_At": "2022-03-10T14:05:29.000Z",
        "recordedVideo": false,
        "thumbnailCreated": true
      },
      "activityType": "uploaded_video"
    }
  ],
  "comment_video": [
    {
      "_id": "622a03e66d59f72b72358069",
      "user_id": "Ec3FNhk45Lh1xk92cO9eoXDiWEk2",
      "comment_video": {
        "_id": "622a0616242e5b2e65e8b3b4",
        "video_description": "test",
        "created_At": "2022-03-10T14:05:29.000Z",
        "recordedVideo": false,
        "thumbnailCreated": true
      },
      "activityType": "comment_video"
    }
  ],
  "like_video": [
    {
      "_id": "622a03e66d59f72b72358069",
      "user_id": "Ec3FNhk45Lh1xk92cO9eoXDiWEk2",
      "like_video": {
        "_id": "622a0616242e5b2e65e8b3b4",
        "video_description": "test",
        "created_At": "2022-03-10T14:05:29.000Z",
        "recordedVideo": false,
        "thumbnailCreated": true
      },
      "activityType": "like_video"
    }
  ]
}

I want all the field data given inside a single field, I tried my best but did not get any solutions, hope anyone can do this, please solve this to aggregate, because I will add some conditions in the latter in aggregation, I want to use aggregation and want the below formate.

    {
  "all_data": [
    {
      "_id": "622a03e66d59f72b72358069",
      "user_id": "Ec3FNhk45Lh1xk92cO9eoXDiWEk2",
      "uploaded_video": {
        "_id": "622a0616242e5b2e65e8b3b4",
        "video_description": "test",
        "created_At": "2022-03-10T14:05:29.000Z",
        "recordedVideo": false,
        "thumbnailCreated": true
      },
      "activityType": "uploaded_video"
    }
    {
      "_id": "622a03e66d59f72b72358069",
      "user_id": "Ec3FNhk45Lh1xk92cO9eoXDiWEk2",
      "comment_video": {
        "_id": "622a0616242e5b2e65e8b3b4",
        "video_description": "test",
        "created_At": "2022-03-10T14:05:29.000Z",
        "recordedVideo": false,
        "thumbnailCreated": true
      },
      "activityType": "comment_video"
    },
    {
      "_id": "622a03e66d59f72b72358069",
      "user_id": "Ec3FNhk45Lh1xk92cO9eoXDiWEk2",
      "like_video": {
        "_id": "622a0616242e5b2e65e8b3b4",
        "video_description": "test",
        "created_At": "2022-03-10T14:05:29.000Z",
        "recordedVideo": false,
        "thumbnailCreated": true
      },
      "activityType": "like_video"
    }
  ]
}

Solution

  • db.collection.aggregate([
      {
        $project: {
          allData: {
            $concatArrays: [
              "$uploaded_video",
              "$comment_video",
              "$like_video"
            ]
          }
        }
      }
    ])
    

    mongoplayground