DEV Community

Nagarajan R
Nagarajan R

Posted on

Answer: convert a $lookup result to an object instead of array

You can just use $unwind. It deconstructs an array field from the input documents to output a document for each element

let query = mongoose.model('Discipline').aggregate([
    {
      $match: {
        project: mongoose.Types.ObjectId(req.params.projectId)
      },
    },
    {
      $lookup: {
        from: "typecategories",
        localField: "typeCategory",
        foreignField: "_id",
        as: "typeCategory"
      }
    },
    {$unwind: '$typeCategory'},
    {
      $project:

Top comments (0)