• <bdo id='4sVA2'></bdo><ul id='4sVA2'></ul>

      <small id='4sVA2'></small><noframes id='4sVA2'>

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

      <tfoot id='4sVA2'></tfoot>
    2. <legend id='4sVA2'><style id='4sVA2'><dir id='4sVA2'><q id='4sVA2'></q></dir></style></legend>

      在投影中将子字段提升到顶层而不列出所有键

      时间:2023-10-02

      • <i id='HcoPp'><tr id='HcoPp'><dt id='HcoPp'><q id='HcoPp'><span id='HcoPp'><b id='HcoPp'><form id='HcoPp'><ins id='HcoPp'></ins><ul id='HcoPp'></ul><sub id='HcoPp'></sub></form><legend id='HcoPp'></legend><bdo id='HcoPp'><pre id='HcoPp'><center id='HcoPp'></center></pre></bdo></b><th id='HcoPp'></th></span></q></dt></tr></i><div id='HcoPp'><tfoot id='HcoPp'></tfoot><dl id='HcoPp'><fieldset id='HcoPp'></fieldset></dl></div>
          <legend id='HcoPp'><style id='HcoPp'><dir id='HcoPp'><q id='HcoPp'></q></dir></style></legend>
        • <small id='HcoPp'></small><noframes id='HcoPp'>

            <bdo id='HcoPp'></bdo><ul id='HcoPp'></ul>
            1. <tfoot id='HcoPp'></tfoot>

                  <tbody id='HcoPp'></tbody>
              1. 本文介绍了在投影中将子字段提升到顶层而不列出所有键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有像 {'a': 1, 'z': {'b': 2, 'c': 3,}} 这样的文件.

                I have documents like {'a': 1, 'z': {'b': 2, 'c': 3,}}.

                我想要 {'a': 1, 'b': 2, 'c': 3}.

                我可以用

                aggregate({'$project': {'b': '$z.b', 'c': '$z.c'}})
                

                是否可以在不手动列出子文档中的所有键的情况下做到这一点?

                Is it possible to do it without listing all of the keys in the subdocument manually?

                推荐答案

                在 MongoDB 3.4 中,您可以使用 $objectToArray$arrayToObject$replaceRoot为了改变这个:

                With MongoDB 3.4 you can use $objectToArray and $arrayToObject with $replaceRoot in order to change this:

                db.wish.aggregate([
                  { "$replaceRoot": {
                    "newRoot": {
                      "$arrayToObject": {
                        "$concatArrays": [
                          [{ "k": "a", "v": "$a" }],
                          { "$objectToArray": "$z" }
                        ]
                      }
                    }
                  }}
                ])
                

                甚至连这么长的咒语都没有指定 "a" 属性:

                Or even this long incantation without even specifying the "a" property:

                db.wish.aggregate([
                  { "$replaceRoot": {
                    "newRoot": {
                      "$arrayToObject": {
                        "$reduce": {
                          "input": {
                            "$filter": {
                              "input": { "$objectToArray": "$$ROOT" },
                              "as": "r",
                              "cond": { "$ne": [ "$$r.k", "_id" ] }
                            }
                          },
                          "initialValue": [],
                          "in": {
                            "$concatArrays": [
                              "$$value",
                              { "$cond": {
                                "if": {  "$gt": [ "$$this.v", {} ] },
                                "then": { "$objectToArray": "$$this.v" },
                                "else": ["$$this"]
                              }}
                            ]
                          }  
                        } 
                      }
                    }
                  }}
                ])  
                

                两者都产生:

                { "a" : 1, "b" : 2, "c" : 3 }
                

                $concatArrays 的有趣用法在未来的版本中应该是不必要的,因为将会有一个 $mergeObjects 操作符,这会使它更简洁一些.

                The funny use of $concatArrays should not be necessary in future versions since there will be a $mergeObjects operator which will make that a bit cleaner.

                但您基本上可以非常简单地在客户端代码中执行相同的操作.例如在 JavaScript 中的 shell:

                But you can basically just do the same thing in client code pretty simply. For example in JavaScript for the shell:

                db.wish.find().map( doc => (
                  Object.assign({ a: doc.a }, doc.z )
                ))
                

                或者是没有"a"的版本:

                db.wish.find().map( doc => 
                  Object.keys(doc).filter(k => k !== '_id').map(k => 
                   ( typeof(doc[k]) === "object" ) ? 
                     Object.keys(doc[k]).map(i => ({ [i]: doc[k][i] }))
                       .reduce((acc, curr) => Object.assign(acc,curr),{})
                     : { [k]: doc[k] }
                  ).reduce((acc,curr) => Object.assign(acc,curr),{})
                )
                

                产生相同的输出

                { "a" : 1, "b" : 2, "c" : 3 }
                

                这篇关于在投影中将子字段提升到顶层而不列出所有键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

              2. <legend id='NqmIg'><style id='NqmIg'><dir id='NqmIg'><q id='NqmIg'></q></dir></style></legend>
              3. <small id='NqmIg'></small><noframes id='NqmIg'>

                  <tfoot id='NqmIg'></tfoot>
                    <tbody id='NqmIg'></tbody>

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