'convert a $lookup result to an object instead of array

I'm doing a $lookup from a _id. So the result is always 1 document. Hence, I want the result to be an object instead an array with one item.

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

This notation: "$typeCategory[0]" is not working. Is there any smart way of doing this?



Solution 1:[1]

You can use $arrayElemAt in $project stage.

Syntax of $arrayElemAt is { $arrayElemAt: [ <array>, <idxexOfArray> ] }

like:

mongoose.model('Discipline').aggregate([
   {
      $match: {
        project: mongoose.Types.ObjectId(req.params.projectId)
      },
   },
   {
      $lookup: {
        from: "typecategories",
        localField: "typeCategory",
        foreignField: "_id",
        as: "typeCategory"
      }
   },
   {
      $project: {
         name: 1, typeCategory: {$arrayElemAt:["$typeCategory",0]}
      }
   }
]);

Solution 2:[2]

For merging two collections:

{$replaceRoot: { newRoot: { $mergeObjects: [ { $arrayElemAt: [ "$typeCategory", 0 ] }, "$$ROOT" ] } }    }, 

{ $project: { typeCategory: 0 } }

Solution 3:[3]

Use $first to return the first element in the array:

$project: {
    title: 1, 
    typeCategory: {$first: "$typeCategory"}
}

Sources

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

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Shaik Matheen
Solution 3 Savrige