Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

mongoose lookup array of objects

db.players.aggregate([
  { $unwind: "$items" },
  {
    $lookup: {
      from: "items",
      let: {
        itemId: { $toObjectId: "$items.itemId" },
        items: "$items"
      },
      pipeline: [
        { $match: { $expr: { $eq: ["$_id", "$$itemId" ] } } },
        { $replaceRoot: { newRoot: { $mergeObjects: ["$$items", "$$ROOT"] } } }
      ],
      as: "items"
    }
  },
  {
    $group: {
      _id: "$_id",
      username: { $first: "$username" },
      items: { $push: { $first: "$items" } }
    }
  }
])
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #mongoose #lookup #array #objects
ADD COMMENT
Topic
Name
9+9 =