【问题标题】:Concat arrays using PyMongo failed with unknown group operator '$concatArrays'使用 PyMongo 的 Concat 数组因未知组运算符“$concatArrays”而失败
【发布时间】:2019-05-25 23:07:31
【问题描述】:

我有 mongodb 数据,例如:

{'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来处理,结果应该是:

{'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 中使用组:

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

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

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:\Users\ll\.virtualenvs\SearchEngine\lib\site-packages\pymongo\collection.py", line 2550, in group  group["$reduce"] = Code(reduce)  File "C:\Users\ll\.virtualenvs\SearchEngine\lib\site-packages\bson\code.py", line 54, in __new__  "instance of %s" % (string_type.__name__))
TypeError: code must be an instance of str

【问题讨论】:

    标签: python mongodb aggregation-framework pymongo


    【解决方案1】:

    您收到此错误消息的原因是因为 $concatArrays 运算符是 expression operator 而不是 $group accumulator

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

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

    我们在$group 阶段使用$push 运算符创建一个信息 的二维列表,然后在另一个$project 阶段您使用$reduce$concatArrays 展平列表.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-27
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      • 2017-07-22
      • 2015-12-26
      相关资源
      最近更新 更多