如何使用python从mongodb查询中过滤重复日志

时间:2016-02-16 09:44:24

标签: python json mongodb pymongo

{u'field': u'error_starting_process', u'_id':ObjectId('56c2ec225141a3121900188e'), u'severity': u'major', u'description': u'Process failed to start.'}

{u'field': u'process_not_running', u'_id': ObjectId('56c2ec226715991f2000197e'), u'severity': u'minor', u'description': u'Process Asset on host  should be running but is not.'}

{u'field': u'process_not_running', u'_id': ObjectId('56c2ec2411debd2b1e0022dd'), u'severity': u'minor', u'description': u'Process Timer Task Execution on host  should be running but is not.'}

{u'field': u'error_starting_process', u'_id': ObjectId('56c2ec24df312f091c0021b0'), u'severity': u'major', u'description': u'Process failed to start.'}

如何过滤重复说明?

1 个答案:

答案 0 :(得分:1)

您可以使用distinct方法,然后检索与每个描述匹配的第一个文档。 e.g:

for desc in db.collection.distinct("description"):
    doc = db.collection.find({"description":desc })[0]