Concat arrays using PyMongo failed with unknown group operator #39;$concatArrays#39;(使用 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"而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 PyMongo 的 Concat 数组因未知组运算符“$concatArrays"而失败
基础教程推荐
- 求两个直方图的卷积 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 包装空间模型 2022-01-01
