How to add single field using $lookup?

Hello @blue_puma Welcome to MongoDB Community Forum,

You can use $lookup without pipeline,

  • $lookup with stati collection, pass status as localField and pass id as foreignField
  • $set to show str status from above lookup result, $arrayElemAt to get first element from lookup result strStatus.str.
db.orders.aggregate([
  {
    $lookup: {
      from: "stati",
      localField: "status",
      foreignField: "id",
      as: "strStatus"
    }
  },
  {
    $set: {
      strStatus: { $arrayElemAt: ["$strStatus.str", 0] }
    }
  }
])
9 Likes