Home > Software design >  nodejs mongodb aggregation not working properly
nodejs mongodb aggregation not working properly

Time:04-25

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

CodePudding user response:

Can you try :

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

CodePudding user response:

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

mongoplayground

CodePudding user response:

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

  • Related