Data Transformation and group

This is my useSchema

const userSchema = new Schema(
  {
    email: {
      type: String,
      required: true,
      unique: true,
    },
    password: {
      type: String,
      required: true,
    },
    userName: {
      type: String,
      unique: true,
    },
    firstName: {
      type: String,
    },
    lastName: {
      type: String,
    },
    currency: {
      type: String,
    },
    promotionCompany: {
      type: String,
    },
    displayNameInPublicRankingPage: {
      type: Boolean,
      default: false,
    },
    surpriceContribution: {
      type: Boolean,
      default: false,
    },
    surpriceContributionAmount: {
      type: String,
    },
    profileImage: {
      type: String,
    },
    bannerImage: {
      type: String,
    },
    setAutoPost: {
      type: Boolean,
      default: false,
    },
    agreeTermConditions: {
      type: Boolean,
      default: false,
    },
    status: {
      type: String,
      default: true,
    },
    role: {
      type: String,
      enum: ["fighter", "companion"],
      default: "fighter",
    },
    followers: [
      {
        type: mongoose.Schema.Types.ObjectId,
        ref: "User",
      },
    ],
    cart: [
      {
        shopType: {
          type: String,
          enum: ["goal", "surprise"],
          default: "goal",
        },
        goal: { type: mongoose.Schema.Types.ObjectId, ref: "Goal" },
        quantity: Number,
        goalType: String,
        amount: String,
        senderMessage: String,
        fighterId: String,
      },
    ],
    socialLinks: [
      {
        platform: {
          type: String,
          enum: [
            "Facebook",
            "Twitter",
            "Instagram",
            "LinkedIn",
            "Tiktok",
            "Other",
          ], // Adjust as needed
          required: true,
        },
        link: {
          type: String,
          required: true,
        },
        status: {
          type: Boolean,
          default: true,
        },
      },
    ],
  },
  {
    timestamps: true,
  }
);

This is my goal schema

const goalSchema = new Schema({
      goalName: {
        type: String,
        required: true,
      },
      goalPrice: {
        type: Number,
        required: true,
      },
      goalType: {
        type: String,
        enum: ['single', 'subscription', 'crowd'],
        default: 'single', 
      },
      goalPurchaseType: {
        type: String,
        enum: ['single', 'multiple'],
        default: 'multiple', 
      },
      goalCategory: {
        type: String,
        required: true,
      },
      goalImage: {
        type: String, 
        required: true,
      },
      status: {
        type: String,
        enum: ['active', 'unactive'],
        default: 'active', 
      },
      creator: {
        type: Schema.Types.ObjectId,
        ref: 'User',
        required: true
      }
 
  },{
    timestamps: true,
});

In this what I want is that when the user requests to fetch the cart I want to group the cart based on fighterId and send the data in this format to the user in response.

{
  "cart": [
    {
      "wisher": {
        "wisher_id": "63325fa1743c2a0004f30b29",
        "name": "Goddess Mariah",
        "userName": "ineeddmariah",
        "profileImage": "https://wishtender.s3.amazonaws.com/images/profileImages/6df12e17-793c-4159-9fb3-3fa7af2d712f.png",
        "Wishercurrency": "USD"
      },
      "cartItem": [
        {
          "id": "cart_id",
          "goalType": "tip",
          "quantity": 1,
          "goalImage": "goal_image",
          "goalPurchaseType": "single",
          "goalName": "example_goal_name",
          "goalPrice": "209"
        }
      ]
    },
    {
      // Additional cart items can be added here if needed
    }
  ]
}

Hi @Rahul_Kumar25, welcome to the community forum!

From what I could get, you can accomplish your idea using an aggregation pipeline with a $group stage.