简体   繁体   English

mongoDB 聚合 $lookup 并从现有数组中推送一些信息

[英]mongoDB aggregate $lookup and push some info from existing array

I have many collections in my MongoDB database.我的 MongoDB 数据库中有许多 collections。 Aggregation work's good but I can't push some fields to the output I needed,聚合工作很好,但我无法将某些字段推送到我需要的 output,

collection A is:集合 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
   }
  ]
}

my query is:我的查询是:

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)

I need to have output like this:我需要像这样的 output :

_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
  },      
]

so, how I can push neededFieldToPush field into prMove object?那么,如何将neededFieldToPush字段推送到prMove object 中?

thank you谢谢你

The implementation of your query is expensive because you have used $unwind stage and $group stage, it will impact performance,查询的实现很昂贵,因为您使用$unwind阶段和$group阶段,它会影响性能,

The second thing is the $sort , $skip and $limit functions will not work on the aggregate function of mongoose, you have use stages for that,第二件事是$sort$skip$limit功能不适用于 mongoose 的聚合 function,您有使用阶段,

The third thing is to use sort, $skip and $limit stages immediately after $match stage, so it will improve performance while you create an index on required fields.第三件事是在$match阶段之后立即使用 sort、 $skip$limit阶段,这样可以在您为必填字段创建索引时提高性能。

Improved query,改进的查询,

  • $match your query $match你的查询
  • $sort sort stage $sort排序阶段
  • $skip stage to pass offset $skip阶段以通过偏移量
  • $limit stage to limit documents $limit阶段限制文件
  • $lookup with colors collection and pass items.color as localField $lookup使用 colors 集合并将items.color作为localField传递
  • $addFields to edit items array with color name in items $addFields以在items中使用颜色名称编辑items数组
  • $map to iterate loop of items array $map迭代items数组的循环
  • $reduce to iterate loop of itemColor array result from lookup, check condition if _id match then get color name and set value in color field $reduce从查找中迭代itemColor数组结果的循环,检查条件是否_id匹配,然后获取颜色名称并在color字段中设置值
  • $mergeObjects to merge new color field and current fields of the item object $mergeObjects合并项目 object 的新color字段和当前字段
  • $$REMOVE to remove itemColor array because it is not needed now $$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操场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM