mongodb.如何通过聚合管道携带计算的字段



我正在尝试以JSON形式从某些OSM数据中收集一些见解。这是我在mongodb/pymongo中使用的文档的示例:

{"amenity": "post_office",
 "name": "Dominion Road Postshop", 
 "created": {"uid": "10829", 
             "changeset": "607706", 
             "version": "5", 
             "user": "myfanwy",   
              "timestamp": "2007-11-24T12:41:04Z"}, 
 "pos": [-36.8801299, 174.7495053], 
 "created_by": "Potlatch 0.5d", 
 "type": "node", 
 "id": "61076379"}

因此,每个文档都有一个用户和设施。我想找到每个用户记录的每种便利设施的计数,除以每个用户记录的总设施的总量。

因此,为了帮助澄清这是我用来查找每个单独计数的代码片段:

查询1.查找每个用户记录的每种便利设施中有多少个:

amenity_per_user = coll.aggregate([{"$match":{"amenity":{"$exists":True}}},
                               {"$group":{"_id":{"user":"$created.user", "amenities":"$amenity"}, "count":{"$sum":1}}},
                               {"$sort":{"count":-1}}])

查询2.找到每个用户记录多少设施:

results = coll.aggregate([{"$match":{"amenity":{"$exists":True}}},
                      {"$group":{"_id":"$created.user", "count":{"$sum":1}}},
                      {"$sort":{"count":-1}}])

,两者的答案都是(每个限制为5个结果):

Finding how many of each amenity each user records:
{u'_id': {u'amenities': u'parking', u'user': u'Rudy355'}, u'count': 1886}
{u'_id': {u'amenities': u'post_box', u'user': u'Rudy355'}, u'count': 547}
{u'_id': {u'amenities': u'telephone', u'user': u'Rudy355'}, u'count': 485}
{u'_id': {u'amenities': u'parking', u'user': u'myfanwy'}, u'count': 451}
{u'_id': {u'amenities': u'restaurant', u'user': u'Rudy355'}, u'count': 429}
Find how many amenities each user records:
{u'_id': u'Rudy355', u'count': 6321}
{u'_id': u'myfanwy', u'count': 951}
{u'_id': u'Robert Ancell', u'count': 599}
{u'_id': u'lcmortensen', u'count': 366}
{u'_id': u'Marks2000', u'count': 228}

现在我想做的是将每个用户的顶级便利设施(即1886年的停车设施中的条目)除以其录音总数(查询2)。 - 因此,最终结果是Rudy355在"停车"便利设施中创作了0.3张录音。-1886/6321 = 0.3。

这就是我要:

coll.aggregate([{"$match":{"amenity":{"$exists":True}}},
                    {"$group":{"_id":"$created.user", "user_count":{"$sum":1}}},
                    {"$group":{"_id":{"user":"$created.user", "amenities":"$amenity"}, "amenity_count":{"$sum":1}, 
                               "ucount":{"$push":"$user_count"}}},
                    {"$unwind":"$ucount"},
                    {"$project":{"$divide":{"$ucount", "$amenity_count"}}},
                    {"$sort":{"count":-1}}])

任何帮助都很棒!

顺便说一句,我真的不喜欢使用$ pupp的想法来节省" user_count"的值。有人知道一种更好的方法来保存类似的计算字段吗?

您可以尝试以下聚合。$push保存每个amenity及其count,以稍后用于使用total用户便利计数来计算record

 [
    {"$match":{"amenity":{"$exists":True}}},
    {"$group":{"_id":{"user":"$created.user", "amenity":"$amenity"}, "count":{"$sum":1}}},
    {"$group":{"_id":"$_id.user", "total":{"$sum":"$count"}, "amenities":{"$push":{amenity:"$_id.amenity","count":"$count"}}}},
    {"$unwind":"$amenities"},
    {"$project:{"_id":0,"user":"$_id", "amenity":"$amenities.amenity", record":{"$divide":{"$amenities.count", "$total"}}}},
    {"$sort":{"record":-1}}
]                                                                                                                           

您应该像下面的输出一样。

{"user":"Rudy355", "amenity":"parking", "record":0.3}

最新更新