Regarding Aggregation

I have two field name, email in an array format

I want to write it in this format

How can I use aggregation for that ,pls help

Hello @Abhishek_Upadhyay1, Welcome to the MongoDB community forum,

You can use something like this pipeline,

  • $map to iterate loop of email
  • $indexOfArray to get the array index of the email
  • $arrayElemAt to get a specific element by using its array index from name
  • the map will produce a key-value pair array of objects
  • $arrayToObject converts the above array of objects into an object
  • $replceRoot to replace the above object in the root
    $project: {
      items: {
        $map: {
          input: "$email",
          in: {
            k: {
              $arrayElemAt: [
                { $indexOfArray: ["$email", "$$this"] }
            v: "$$this"
    $replaceRoot: {
      newRoot: { $arrayToObject: "$items" }