【问题标题】:mongoDB aggregate $lookup and push some info from existing arraymongoDB 聚合 $lookup 并从现有数组中推送一些信息
【发布时间】:2022-01-12 15:31:54
【问题描述】:

我的 MongoDB 数据库中有很多集合。 聚合工作很好,但我无法将某些字段推送到我需要的输出,

集合 A 是:

{
  _id: some mongodbID
  //...fields
  items: [
   {
     _id: someId,
     color: someId <---- Im aggregate this with lookup
     neededFieldToPush: 123
   },
   {
     _id: someId,
     color: someId <---- Im aggregate this with lookup
     neededFieldToPush: 566
   }
  ]
}

我的查询是:

await Invoice.aggregate([
  { $match: query },
  { $unwind: "$items" },
  //colors
  {
    $lookup: {
      from: "colors",
      localField: "items.itemColor",
      foreignField: "_id",
      as: "itemColor"
    }
   },
   {
     $addFields: {
       "prMove.itemColor": { $arrayElemAt: ["$itemColor.colorName", 0] },
     }
    },
    {
      $group: {
        _id: "$_id",
        items: { $push: "$items" }, <-- original items
        prMove: { $push: "$prMove" },
       }
     },
   ])
   .sort({date: -1})
   .skip(+req.query.offset)
   .limit(+req.query.limit)

我需要这样的输出:

_id: someId,
items: [//original items],
prMove: [
  {
    itemColor: some color name, <--- it's works fine
    neededFieldToPush: 123
  },
  {
    itemColor: some color name, <--- it's works fine
    neededFieldToPush: 566
  },      
]

那么,我如何将neededFieldToPush 字段推入prMove 对象?

谢谢

【问题讨论】:

    标签: mongodb mongoose aggregation-framework


    【解决方案1】:

    您的查询的实现成本很高,因为您使用了$unwind 阶段和$group 阶段,它会影响性能,

    第二件事是$sort$skip$limit函数在mongoose的聚合函数上不起作用,你有使用阶段,

    第三件事是在$match 阶段之后立即使用排序、$skip$limit 阶段,这样在为必填字段创建索引时会提高性能。

    改进的查询,

    • $match您的查询
    • $sort排序阶段
    • $skip 阶段通过偏移量
    • $limit阶段限制文件
    • $lookup 带有颜色集合并通过 items.color 作为 localField
    • $addFields 编辑 items 数组,颜色名称在 items
    • $map 迭代 items 数组的循环
    • $reduce 从查找中迭代 itemColor 数组结果的循环,检查条件是否 _id 匹配然后获取颜色名称并在 color 字段中设置值
    • $mergeObjects 合并新的color 字段和项目对象的当前字段
    • $$REMOVE 删除 itemColor 数组,因为现在不需要它
    let offset = req.query.offset;
    let limit = req.query.limit;
    await Invoice.aggregate([
      { $match: query },
      { $sort: { date: -1 } },
      { $skip: offset },
      { $limit: limit },
      {
        $lookup: {
          from: "colors",
          localField: "items.color",
          foreignField: "_id",
          as: "itemColor"
        }
      },
      {
        $addFields: {
          items: {
            $map: {
              input: "$items",
              as: "i",
              in: {
                $mergeObjects: [
                  "$$i",
                  {
                    color: {
                      $reduce: {
                        input: "$itemColor",
                        initialValue: "",
                        in: {
                          $cond: [
                            { $eq: ["$$this._id", "$$i._id"] },
                            "$$this.colorName",
                            "$$value"
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          },
          itemColor: "$$REMOVE"
        }
      }
    ])
    

    Playground

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      • 2022-11-22
      • 1970-01-01
      • 1970-01-01
      • 2019-03-23
      • 1970-01-01
      相关资源
      最近更新 更多