如何检索MongoDB中特定字段的所有值?
我有Twitter API的数据,如果国家码等于“ AU”,我想检索所有城市的列表。
{
"gnip" : {
"matching_rules" : [
{
"value" : "bio_location: \"Australia\"",
"tag" : null
},
{
"value" : "bio_location: \"Canberra\"",
"tag" : null
}
],
"klout_score" : 40,
"language" : {
"value" : "en"
},
"profileLocations" : [
{
"objectType" : "place",
"geo" : {
"type" : "point",
"coordinates" : [
149.12807,
-35.28346
]
},
"address" : {
"country" : "Australia",
"countryCode" : "AU",
"locality" : "Canberra",
"region" : "Australian Capital Territory"
},
"displayName" : "Canberra, Australian Capital Territory, Australia"
}
]
},
}
在这里重要的是要注意,在这里不同的()方法还不够,因为我不想拥有独特的城市名称,而是每个推文的每个城市,如果它位于澳大利亚。
输出应该看起来像这样: [ 悉尼, 墨尔本, 克兰伯拉 这是给出的
I have data from the Twitter API and I want to retrieve a list of all cities if the countryCode is equal to "AU".
{
"gnip" : {
"matching_rules" : [
{
"value" : "bio_location: \"Australia\"",
"tag" : null
},
{
"value" : "bio_location: \"Canberra\"",
"tag" : null
}
],
"klout_score" : 40,
"language" : {
"value" : "en"
},
"profileLocations" : [
{
"objectType" : "place",
"geo" : {
"type" : "point",
"coordinates" : [
149.12807,
-35.28346
]
},
"address" : {
"country" : "Australia",
"countryCode" : "AU",
"locality" : "Canberra",
"region" : "Australian Capital Territory"
},
"displayName" : "Canberra, Australian Capital Territory, Australia"
}
]
},
}
It is important to note here that the distinct() method is not sufficient here since I do not want to have the distinct city names, but each city for each tweet if it is located in Australia.
the output should look like this:
[
Sydney,
Melbourne,
Cranberra
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下汇总查询将不同的城市名称作为聚合结果返回。
$ filter
聚合操作员用于选择带有country代码的数组数据为“ au”,$ map
用于仅获取地址局部性值。$ setIntersection
是要使局部性名称不同。注意,聚合输出始终是具有文档的光标。示例输出:
{“结果”:[“堪培拉”,“悉尼”]}
。要从输出中获取数组,请执行此操作:
The following aggregation query returns the distinct city names as aggregation result.
The
$filter
aggregation operator is used to select the array data with country code as "AU", and the$map
is used for getting just the address locality values. The$setIntersection
is to have the locality names distinct.Note the aggregation output is always a cursor with document(s). Example output:
{ "result" : [ "Canberra", "Sydney" ] }
.To get just the array from the output, do this:
mongoplayground
mongoplayground