【发布时间】:2019-09-01 04:39:41
【问题描述】:
好吧,我想创建某种 MapReduce 算法来为文本文档创建反向索引。 在映射部分,我做了这样的事情
letters = ['a']
regx = re.compile("^("+"|".join(letters)+')')
selectedWords = directIndex.aggregate([
{ "$match": { "words.word": regx } },
{ "$unwind": "$words" },
{ "$match": { "words.word": regx } },
{ "$group": { "_id": { "word":"$words.word", "count":"$words.count", 'document' : '$document' } } }])
好吧,在这里,我按首字母选择所有与之相关的单词和信息。在此之后,我将此信息写入另一个集合:
myinvcol.insert_one({'letter':str(''.join(letters)),'words':selectedWords })
在下一步中,我正在读取每个插入的文档并执行缩减操作 dict('wordName':{documents:[document1:count1, document2:count2, etc], 'wordName2:{documents:[...] }') 并对这个字典做一些额外的操作
现在,有趣的部分)): 是否可以在 MongoDB 服务器上完全执行第一步(地图部分),也就是聚合?换句话说,我知道有 '$out' 运算符:
letters = ['a']
regx = re.compile("^("+"|".join(letters)+')')
selectedWords = directIndex.aggregate([
{ "$match": { "words.word": regx } },
{ "$unwind": "$words" },
{ "$match": { "words.word": regx } },
{ "$group": { "_id": { "word":"$words.word", "count":"$words.count", 'document' : '$document' } } }
{ "$out" : 'InverseIndex'}])
它允许我将聚合结果写入另一个集合,但它不能做我想要的:而不是插入一个文档:
{'letter':str(''.join(letters)),'words':selectedWords },
我有很多插入
{ "_id": { "word":"$words.word", "count":"$words.count", 'document' : '$document' } }.
那么,最后,有没有一种方法可以在聚合中创建一个文档,在 $out 语句之前将其所有结果合并到一个数组中?
【问题讨论】:
标签: python-3.x mongodb mapreduce aggregate pymongo