【发布时间】:2015-02-05 23:18:14
【问题描述】:
在我的数据库中,我的文档具有 location 属性,其数据如下:
{location: {Country: 'Nigeria', State: 'Lagos', City: 'Victoria Island'}}
请注意,“Country”、“State”和“City”是用户定义的键/属性/属性。用户可以使用图形界面来创建位置类型(键/属性/属性),因为它们可能包含空格或以其他方式对点表示法无效。
我目前在聚合中使用$project 步骤,如下所示:
{$project: {location: '$location.{}'.format(location_type)}}
但我想将其转换为嵌套形式。到目前为止,做的事情像
{location: {'$location': {'{}'.format(location_type): 1}}}
没用,因为$location 不是公认的聚合运算符,而且我对 MongoDB 和聚合还比较陌生。
关于预期输出,我只投影选定的location_type,以便我可以按那个分组,所以我基本上想通过指定的location_type 来'$group,例如,我可以投影国家或城市或州(再次,纯粹是为了举例 - 位置类型是用户定义的,因此它们可能被称为其他任何东西)按此分组。
举例来说,如果location_type 是“国家”,则输出将为{location: 'Nigeria'},如果location_type 为“州”,则输出将为{location: 'Lagos'},如果location_type 为“城市” ',对于上面的示例文档,输出将是 {location: 'Victoria Island'}。
【问题讨论】:
-
预期输出是什么??
-
请添加一个清晰的示例,说明点符号不起作用的情况以及所需的输出。
标签: mongodb pymongo aggregation-framework