Q: MongoDB get fieldvalue based on fieldname I'm new to MongoDB, and I'm not sure if what I'm trying to do is possible at all. I've exhausted my Google skills, so I'm hoping someone here can give me a push in the right direction.
My data is structured like this:
db={
  "people": [
    {
      "id": 1,
      "name": "Pete",
      "occupation": "baker"
    },
    {
      "id": 2,
      "name": "Mike",
      "occupation": "painter"
    }
  ],
  "activity": [
    {
      "baker": "bakes",
      "painter": "paints"
    }
  ]
}

Although this is just sample data, my "activity" is a single large document with unique keys that I'm trying to get the value from, based on key name (when it matches value from the "people" document).
What I'm trying to achieve is this output:
[
  {
    "id": 1,
    "name": "Pete",
    "occupation": "baker",
    "activity:" "bakes"
  },
  {
    "id": 2,
    "name": "Mike",
    "occupation": "painter",
    "activity": "paints"
  }
]

Is this possible at all?

A: If I understand your database and data model, it seems like a poor model.
If the data model was different (see below), the query would be quite simple.  Given that, here's one way you could generate your desired output with your model.
db.people.aggregate([
  { "$lookup": {
      "from": "activity",
      "let": { occ: "$occupation" },
      "pipeline": [
        {
          "$replaceWith": {
            "$arrayToObject": {
              "$filter": {
                "input": { "$objectToArray": "$$ROOT" },
                "as": "kv",
                "cond": { "$eq": [ "$$kv.k", "$$occ" ] }
              }
            }
          }
        }
      ],
      "as": "activity"
    }
  },
  { "$set": {
      "activity": {
        "$getField": {
          "field": "v",
          "input": {
            "$first": {
              "$objectToArray": { "$first": "$activity" }
            }
          }
        }
      }
    }
  },
  { "$unset": "_id" }
])

Try it on mongoplayground.net.
If your activity collection has this model...
[
  {
    "occupation": "baker",
    "activity": "bakes"
  },
  {
    "occupation": "painter",
    "activity": "paints"
  }
]

... then the query could be:
db.people.aggregate([
  { "$lookup": {
      "from": "activity",
      "localField": "occupation",
      "foreignField": "occupation",
      "as": "activity"
    }
  },
  { "$set": { "activity": { "$first": "$activity.activity" } } },
  { "$unset": "_id" }
])

Try it on mongoplayground.net.
