K
K
kaxa32012020-11-19 17:10:53
MongoDB
kaxa3201, 2020-11-19 17:10:53

How to merge records by the same values?

Part of my request

{
      "$group": {
         "_id": {
            "stage_name": "$stage_name",
            "user_id": {
               $cond: {
                  if: {
                     $eq: [
                        "$stage_name",
                        "first"
                     ]
                  },
                  then: "$output_payload.user_id",
                  else: "$user_id"
               }
            },

            "period": {
               $month: "$created_at"
            },
         },
         "result": {
            "$sum": 1
         }
      }
   },
   {
      "$project": {
         '_id': 0,
         "user_id": '$_id.user_id',
         "stage_name": '$_id.stage_name',
         "period": '$_id.period',
         "result": "$result"
      }
   },


Displays
{
    "user_id": {"$binary": 821ef4ee-bc36-3d6e-bdf6-8247ccd3281b},
    "period": 10,
    "result": 7168,
    "stage_name": "first"
  },
  {
    "user_id": {"$binary": 821ef4ee-bc36-3d6e-bdf6-8247ccd3281b},
    "period": 10,
    "result": 7306,
    "stage_name": "second"
  }


How can I merge everything into one entry by user_id ?
{
    "user_id":"802ef4ee-bc36-3d6e-bdf6-8247ccd3281b",
    "period":"10",
    "first":7168,
    "second":7306,
 }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question