如何检索MongoDB中特定字段的所有值?

发布于 2025-01-21 08:52:13 字数 1100 浏览 0 评论 0原文

我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

找回味觉 2025-01-28 08:52:13

以下汇总查询将不同的城市名称作为聚合结果返回。

db.collection.aggregate([
{ 
    $project: {
        _id: 0, 
        result: {
            $let: {
                vars: {
                    filtered: { 
                        $filter: { 
                             input: "$gnip.profileLocations", 
                             cond: { 
                                 $eq: [ "$this.address.countryCode", "AU" ] 
                             }
                        }
                    }
                },
                in: {
                    $setIntersection: [ 
                        { 
                            $map: { 
                                 input: "$filtered", 
                                 in: "$this.address.locality"
                            }
                        }
                    ]
                }
            }
        }
    }
}
])

$ filter聚合操作员用于选择带有country代码的数组数据为“ au”,$ map用于仅获取地址局部性值。 $ setIntersection是要使局部性名称不同。

注意,聚合输出始终是具有文档的光标。示例输出:{“结果”:[“堪培拉”,“悉尼”]}

要从输出中获取数组,请执行此操作:

var agg_output = db.collection.aggregate( // substitute pipeline here from above qyery).toArray()
var output = (agg_output.length > 0) ? agg_output[0].result : [ ]  
// [ "Canberra", "Sydney" ]

The following aggregation query returns the distinct city names as aggregation result.

db.collection.aggregate([
{ 
    $project: {
        _id: 0, 
        result: {
            $let: {
                vars: {
                    filtered: { 
                        $filter: { 
                             input: "$gnip.profileLocations", 
                             cond: { 
                                 $eq: [ "$this.address.countryCode", "AU" ] 
                             }
                        }
                    }
                },
                in: {
                    $setIntersection: [ 
                        { 
                            $map: { 
                                 input: "$filtered", 
                                 in: "$this.address.locality"
                            }
                        }
                    ]
                }
            }
        }
    }
}
])

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:

var agg_output = db.collection.aggregate( // substitute pipeline here from above qyery).toArray()
var output = (agg_output.length > 0) ? agg_output[0].result : [ ]  
// [ "Canberra", "Sydney" ]
一刻暧昧 2025-01-28 08:52:13
db.collection.aggregate([
  {
    $match: {
      "gnip.profileLocations.address.countryCode": "AU"
    }
  },
  {
    $unwind: "$gnip.profileLocations"
  },
  {
    $match: {
      "gnip.profileLocations.address.countryCode": "AU"
    }
  },
  {
    $group: {
      _id: null,
      results: {
        $addToSet: "$gnip.profileLocations.address.locality"
      }
    }
  }
])

mongoplayground

db.collection.aggregate([
  {
    $match: {
      "gnip.profileLocations.address.countryCode": "AU"
    }
  },
  {
    $unwind: "$gnip.profileLocations"
  },
  {
    $match: {
      "gnip.profileLocations.address.countryCode": "AU"
    }
  },
  {
    $group: {
      _id: null,
      results: {
        $addToSet: "$gnip.profileLocations.address.locality"
      }
    }
  }
])

mongoplayground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文