• <bdo id='5oDrB'></bdo><ul id='5oDrB'></ul>

        <small id='5oDrB'></small><noframes id='5oDrB'>

        <tfoot id='5oDrB'></tfoot>

      1. <i id='5oDrB'><tr id='5oDrB'><dt id='5oDrB'><q id='5oDrB'><span id='5oDrB'><b id='5oDrB'><form id='5oDrB'><ins id='5oDrB'></ins><ul id='5oDrB'></ul><sub id='5oDrB'></sub></form><legend id='5oDrB'></legend><bdo id='5oDrB'><pre id='5oDrB'><center id='5oDrB'></center></pre></bdo></b><th id='5oDrB'></th></span></q></dt></tr></i><div id='5oDrB'><tfoot id='5oDrB'></tfoot><dl id='5oDrB'><fieldset id='5oDrB'></fieldset></dl></div>
        <legend id='5oDrB'><style id='5oDrB'><dir id='5oDrB'><q id='5oDrB'></q></dir></style></legend>
      2. 使用 PyMongo 的 Concat 数组因未知组运算符“$concatArrays"而失败

        时间:2023-08-30

        <tfoot id='ttoDO'></tfoot>
        <legend id='ttoDO'><style id='ttoDO'><dir id='ttoDO'><q id='ttoDO'></q></dir></style></legend>

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

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

              <bdo id='ttoDO'></bdo><ul id='ttoDO'></ul>

                  <tbody id='ttoDO'></tbody>
                • 本文介绍了使用 PyMongo 的 Concat 数组因未知组运算符“$concatArrays"而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有 mongodb 数据,例如:

                  I have mongodb data like:

                  {'word': 'good', 'info': [{'tbl_id': 'd1', 'term_freq': 2}, {'tbl_id': 'd2', 'term_freq': 56}, {'tbl_id': 'd3', 'term_freq': 3}]}
                  {'word': 'spark', 'info': [{'tbl_id': 'd1', 'term_freq': 6}, {'tbl_id': 'd3', 'term_freq': 11}, {'tbl_id': 'd4', 'term_freq': 10}]}
                  {'word': 'good', 'info': [{'tbl_id': 'd4', 'term_freq': 12}, {'tbl_id': 'd5', 'term_freq': 8}, {'tbl_id': 'd8', 'term_freq': 7}]}
                  {'word': 'spark', 'info': [{'tbl_id': 'd5', 'term_freq': 6}, {'tbl_id': 'd6', 'term_freq': 11}, {'tbl_id': 'd7', 'term_freq': 10}]}
                  

                  我想用pymongo来处理,结果应该是:

                  and I want to use pymongo to process it, the result should be:

                  {'word': 'good',
                   'info': [{'tbl_id': 'd1', 'term_freq': 2}, {'tbl_id': 'd2', 'term_freq': 56}, {'tbl_id': 'd3', 'term_freq': 3},
                            {'tbl_id': 'd4', 'term_freq': 12}, {'tbl_id': 'd5', 'term_freq': 8}, {'tbl_id': 'd8', 'term_freq': 7}]}
                  {'word': 'spark',
                   'info': [{'tbl_id': 'd1', 'term_freq': 6}, {'tbl_id': 'd3', 'term_freq': 11}, {'tbl_id': 'd4', 'term_freq': 10},
                            {'tbl_id': 'd5', 'term_freq': 6}, {'tbl_id': 'd6', 'term_freq': 11}, {'tbl_id': 'd7', 'term_freq': 10}]}
                  

                  我在 pymongo 中使用组:

                  I use group in pymongo:

                  a = mycol.aggregate([{"$group": {"_id":"$word", 'infos': {"$concatArrays": 1}}}])
                  for i in a:
                      print(i)
                  

                  出错了:pymongo.errors.OperationFailure: unknown group operator '$concatArrays'.我使用 group 关键字:

                  It went wrong: pymongo.errors.OperationFailure: unknown group operator '$concatArrays'. and I use group keyword:

                  a = mycol.group(key='word',condition=None, initial={'infos': []}, reduce={"$concatArrays": "info"})
                  for i in a:
                      print(i)
                  

                  也出错了:

                  Traceback (most recent call last):File "F:/programs/SearchEngine/test.py", line 167, in <module> a = mycol.group(key='word',condition=None, initial={'infos': []}, reduce={"$concatArrays": "info"})  File "C:Usersll.virtualenvsSearchEnginelibsite-packagespymongocollection.py", line 2550, in group  group["$reduce"] = Code(reduce)  File "C:Usersll.virtualenvsSearchEnginelibsite-packagessoncode.py", line 54, in __new__  "instance of %s" % (string_type.__name__))
                  TypeError: code must be an instance of str
                  

                  推荐答案

                  您收到此错误消息的原因是 $concatArrays 运算符是 表达式运算符 不是 $group accumulator.

                  The reason you are getting this error message is because the $concatArrays operator is an expression operator not a $group accumulator.

                  话虽如此,您可以使用以下管道执行此操作:

                  That being said, you can do this with the following pipeline:

                  [
                      {
                          "$group": {
                              "_id": "$word",
                              "info": {
                                  "$push": "$info"
                              }
                          }
                      },
                      {
                          "$project": {
                              "_id": 0,
                              "word": "$_id",
                              "info": {
                                  "$reduce": {
                                      "input": "$info",
                                      "initialValue": [
                  
                                      ],
                                      "in": {
                                          "$concatArrays": [
                                              "$$value",
                                              "$$this"
                                          ]
                                      }
                                  }
                              }
                          }
                      }
                  ]
                  

                  我们在 $group 阶段使用 $push 操作符创建一个 info 的二维列表,然后在另一个 $project 阶段你使用 $reduce$concatArrays.

                  We create a 2d list of info in the $group stage with the $push operator then in the another $project stage you flatten the list using the $reduce and $concatArrays.

                  这篇关于使用 PyMongo 的 Concat 数组因未知组运算符“$concatArrays"而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 MongoDB 聚合框架计算一阶导数 下一篇:如何为数组值编写匹配条件?

                  相关文章

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

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

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