• <bdo id='U9nYi'></bdo><ul id='U9nYi'></ul>

    1. <legend id='U9nYi'><style id='U9nYi'><dir id='U9nYi'><q id='U9nYi'></q></dir></style></legend>
      <tfoot id='U9nYi'></tfoot>

      <small id='U9nYi'></small><noframes id='U9nYi'>

    2. <i id='U9nYi'><tr id='U9nYi'><dt id='U9nYi'><q id='U9nYi'><span id='U9nYi'><b id='U9nYi'><form id='U9nYi'><ins id='U9nYi'></ins><ul id='U9nYi'></ul><sub id='U9nYi'></sub></form><legend id='U9nYi'></legend><bdo id='U9nYi'><pre id='U9nYi'><center id='U9nYi'></center></pre></bdo></b><th id='U9nYi'></th></span></q></dt></tr></i><div id='U9nYi'><tfoot id='U9nYi'></tfoot><dl id='U9nYi'><fieldset id='U9nYi'></fieldset></dl></div>

      MongoDB在具有附加字段的对象数组上聚合$lookup

      MongoDB Aggregate $lookup on array of object with additional field(MongoDB在具有附加字段的对象数组上聚合$lookup)

        • <bdo id='xZByZ'></bdo><ul id='xZByZ'></ul>
            <tbody id='xZByZ'></tbody>

          <small id='xZByZ'></small><noframes id='xZByZ'>

            <i id='xZByZ'><tr id='xZByZ'><dt id='xZByZ'><q id='xZByZ'><span id='xZByZ'><b id='xZByZ'><form id='xZByZ'><ins id='xZByZ'></ins><ul id='xZByZ'></ul><sub id='xZByZ'></sub></form><legend id='xZByZ'></legend><bdo id='xZByZ'><pre id='xZByZ'><center id='xZByZ'></center></pre></bdo></b><th id='xZByZ'></th></span></q></dt></tr></i><div id='xZByZ'><tfoot id='xZByZ'></tfoot><dl id='xZByZ'><fieldset id='xZByZ'></fieldset></dl></div>
              <tfoot id='xZByZ'></tfoot>

              • <legend id='xZByZ'><style id='xZByZ'><dir id='xZByZ'><q id='xZByZ'></q></dir></style></legend>
              • 本文介绍了MongoDB在具有附加字段的对象数组上聚合$lookup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有两个不同的集合(下面的示例)方法 &项目.至于现在,我正在为 $lookup 使用 pre 3.6 vanilla 聚合查询:

                I have two different collections (example below) methods & items. As for now, I'm using pre 3.6 vanilla aggregation query for $lookup:

                MongoPlayground 示例

                  {
                    $lookup: {
                      from: "items",
                      localField: "reagents._id",
                      foreignField: "_id",
                      as: "reagent_items"
                    }
                  }
                

                问题是,如果我使用它,我会在 $lookup 阶段错过 quantity 字段(来自嵌入的 methods.reagents)原创收藏.现在,我在 lookup 之后立即返回 quantity 但正如我听说的那样,Mongo 从 3.6 引入了一种用于 lookup 查询的新语法,所以问题是:

                The problem is that if I am using it, I miss quantity field (from methods.reagents embedded) during $lookup stage from original collection. For now, I return quantity right after lookup but as I heard, Mongo introduced from 3.6 a new syntax for lookup queries, so the question is:

                它能否解决我收到以下结果的问题:

                Can it solve my problem for receiving the following results:

                  {
                    "_id": 1,
                    "name": "Test",
                    "reagent_items": [ // <= the exact schema of what I need after lookup
                      {
                        "_id": 1,
                        "name": "ItemOne",
                        "other": "field",
                        "quantity": 2 //quantity field from original {array of objects} after lookup
                      },
                      {
                        "_id": 2,
                        "name": "ItemTwo",
                        "other": "field",
                        "quantity": 4 //quantity field from original {array of objects} after lookup
                      }
                    ],
                    "reagents": [ //original reagents field here just for example, we could remove it
                      {
                        "_id": 1,
                        "quantity": 2
                      },
                      {
                        "_id": 2,
                        "quantity": 4
                      }
                    ]
                  }
                
                

                方法

                    {
                      "_id": 1,
                      "name": "Test",
                      "reagents": [
                        {
                          _id: 1,
                          quantity: 2
                        },
                        {
                          _id: 2,
                          quantity: 4
                        }
                      ]
                    }
                

                项目

                    {
                      "_id": 1,
                      "name": "ItemOne",
                      "other": "field"
                    },
                    {
                      "_id": 2,
                      "name": "ItemTwo",
                      "other": "field"
                    }
                

                推荐答案

                使用 $map 以及 $arrayElemAt 为每个 reagent_items 找到对应的 reagent 并应用 $mergeObjects 获取一个对象:

                Use $map along with $arrayElemAt to find corresponding reagent for each reagent_items and the apply $mergeObjects to get one object:

                db.methods.aggregate([
                    {
                        $lookup: {
                            from: "items",
                            localField: "reagents._id",
                            foreignField: "_id",
                            as: "reagent_items"
                        }
                    },
                    {
                        $project: {
                            _id:1,
                            name: 1,
                            reagents: 1,
                            reagent_items: {
                                $map: {
                                    input: "$reagent_items",
                                    as: "ri",
                                    in: {
                                        $mergeObjects: [
                                            "$$ri",
                                            {
                                                $arrayElemAt: [ { $filter: { input: "$reagents", cond: { $eq: [ "$$this._id", "$$ri._id" ] } } }, 0 ]
                                            }
                                        ]
                                    }
                                }
                            }
                        }
                    }
                ])
                

                Mongo 游乐场

                这篇关于MongoDB在具有附加字段的对象数组上聚合$lookup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))
                • <bdo id='emOMW'></bdo><ul id='emOMW'></ul>
                • <tfoot id='emOMW'></tfoot>

                    <small id='emOMW'></small><noframes id='emOMW'>

                      <tbody id='emOMW'></tbody>
                  • <i id='emOMW'><tr id='emOMW'><dt id='emOMW'><q id='emOMW'><span id='emOMW'><b id='emOMW'><form id='emOMW'><ins id='emOMW'></ins><ul id='emOMW'></ul><sub id='emOMW'></sub></form><legend id='emOMW'></legend><bdo id='emOMW'><pre id='emOMW'><center id='emOMW'></center></pre></bdo></b><th id='emOMW'></th></span></q></dt></tr></i><div id='emOMW'><tfoot id='emOMW'></tfoot><dl id='emOMW'><fieldset id='emOMW'></fieldset></dl></div>

                        1. <legend id='emOMW'><style id='emOMW'><dir id='emOMW'><q id='emOMW'></q></dir></style></legend>