使用 Pymongo 对文本字段进行分组和组合
Group and Combine Text Fields Using Pymongo
我有一组用户评论,我正在尝试合并用户的所有评论,这样我就可以 运行 对它们进行一些 NLP 分析。这感觉应该很容易,但我遗漏了一些关于 Mongo 如何处理字符串的东西。
我的文档是这样的:
{'_id': ObjectId('57e079d3e3874f12ad721f70'),
'atmosphere': 5,
'review_id': 63,
'dedication': 3,
'orgName': 'Some Organization',
'enabled': True,
'accessibility': 3,
'efficiency': 3,
'orgId': '57e05e0de3874f121d516616',
'user': '5809f2c0bc0a53eb49eac583',
'date': '10/20/15 0:00',
'quality': 3,
'orgId_orig': 1098,
'description': 'Here is some sample text'
}
我试过这个:
agg_result = revs.aggregate( [
{ "$group": { "_id": "$user", "mergedText": { "$mergeObjects": "$description" } } }
])
for i in agg_result:
print(i)
但是我收到了这个错误:
OperationFailure: $mergeObjects requires object inputs, but input "Here is some sample text" is of type string
我的预期输出是
{
'userId1':{'mergedText':'joined descriptions from this user'},
'userId2':{'mergedText':'this users descriptions'},
'userId3':{'mergedText':'all descriptions from this user'}
}
其中各种用户 ID 是来自 'user' 字段的 Mongo 对象 ID。
我是 Mongo 的新手,这让我困惑了一段时间。谢谢。
试试这个,合并对象需要对象但是你的描述是你可以推入数组的字符串
agg_result = revs.aggregate( [
{ "$group": { "_id": "$user", "mergedText": { "$push": "$description" } } }
])
for i in agg_result:
print(i)
我有一组用户评论,我正在尝试合并用户的所有评论,这样我就可以 运行 对它们进行一些 NLP 分析。这感觉应该很容易,但我遗漏了一些关于 Mongo 如何处理字符串的东西。
我的文档是这样的:
{'_id': ObjectId('57e079d3e3874f12ad721f70'),
'atmosphere': 5,
'review_id': 63,
'dedication': 3,
'orgName': 'Some Organization',
'enabled': True,
'accessibility': 3,
'efficiency': 3,
'orgId': '57e05e0de3874f121d516616',
'user': '5809f2c0bc0a53eb49eac583',
'date': '10/20/15 0:00',
'quality': 3,
'orgId_orig': 1098,
'description': 'Here is some sample text'
}
我试过这个:
agg_result = revs.aggregate( [
{ "$group": { "_id": "$user", "mergedText": { "$mergeObjects": "$description" } } }
])
for i in agg_result:
print(i)
但是我收到了这个错误:
OperationFailure: $mergeObjects requires object inputs, but input "Here is some sample text" is of type string
我的预期输出是
{
'userId1':{'mergedText':'joined descriptions from this user'},
'userId2':{'mergedText':'this users descriptions'},
'userId3':{'mergedText':'all descriptions from this user'}
}
其中各种用户 ID 是来自 'user' 字段的 Mongo 对象 ID。
我是 Mongo 的新手,这让我困惑了一段时间。谢谢。
试试这个,合并对象需要对象但是你的描述是你可以推入数组的字符串
agg_result = revs.aggregate( [
{ "$group": { "_id": "$user", "mergedText": { "$push": "$description" } } }
])
for i in agg_result:
print(i)