skip to Main Content

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"
    }
  ]
}

3

Answers


  1. Can you try :

    db.collection.aggregate([
      {
        $project: {
          allData: {
            uploaded_video: "$uploaded_video",
            comment_video: "$comment_video",
            like_video: "$like_video"
          }
        }
      },
    ])
    
    Login or Signup to reply.
  2. db.collection.aggregate([
      {
        $project: {
          allData: {
            $concatArrays: [
              "$uploaded_video",
              "$comment_video",
              "$like_video"
            ]
          }
        }
      }
    ])
    

    mongoplayground

    Login or Signup to reply.
  3. You can use $addFields and $concatArrays to get your required format. You can get the demo code here. Hope this helps you out.

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