使用PyMongo的Concat阵列失败,原因是未知的组运算符'$ concatArrays' [英] Concat arrays using PyMongo failed with unknown group operator '$concatArrays'

查看:72
本文介绍了使用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:\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

推荐答案

收到此错误消息的原因是因为$concatArrays运算符是

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阶段中,使用

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'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆