'Transform objects into key/value array mongodb

I have this data:

[
  {
    "_id": "0001",
    "n1": "0001",
    "n2": 1234
  },
  {
    "_id": "0002",
    "n1": "0002",
    "n2": 9876
  }
]

What's the best way to transform it into this, using aggregation framework?:

[
  {
    "_id": "0001",
    "lookupFields": [
      {
        "n": "n1",
        "v": "0001"
      },
      {
        "n": "n2",
        "v": 1234
      }
    ]
  },
  {
    "_id": "0002",
    "lookupFields": [
      {
        "n": "n1",
        "v": "0002"
      },
      {
        "n": "n2",
        "v": 9876
      }
    ]
  }
]

Thanks for your precious help



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source