How can I group data while fetching from mongodb using go?

huangapple go评论90阅读模式
英文:

How can I group data while fetching from mongodb using go?

问题

我有一个名为"myplace"的集合,它有以下字段:place_name(地点名称)、city(城市)、latitude(纬度)、longitude(经度)。

文档格式如下:

{
    "_id" : ObjectId("544a2147785b707b340ed6c7"),
    "latitude" : 12.36547,
    "longitude" : 1.235689,
    "place_name" : "some_place",
    "city" : "City1"
}
{
    "_id" : ObjectId("544a2147785b707b340ed6c7"),
    "latitude" : 12.36547,
    "longitude" : 1.235689,
    "place_name" : "some_place",
    "city" : "City3"
}
{
    "_id" : ObjectId("544a2147785b707b340ed6c7"),
    "latitude" : 12.36547,
    "longitude" : 1.235689,
    "place_name" : "some_place",
    "city" : "City1"
}
{
    "_id" : ObjectId("544a2147785b707b340ed6c7"),
    "latitude" : 12.36547,
    "longitude" : 1.235689,
    "place_name" : "some_place",
    "city" : "City2"
}
{
    "_id" : ObjectId("544a2147785b707b340ed6c7"),
    "latitude" : 12.36547,
    "longitude" : 1.235689,
    "place_name" : "some_place",
    "city" : "City2"
}

如何将具有相同城市的数据分组?意思是我需要一个包含所有具有City1的数据的数组,第二个数组包含所有具有City2的数据,依此类推。

英文:

I have a collection named "myplace" It has the following fileds place_name, city, latitude, longitude.

Doc format

{
      "_id" : ObjectId("544a2147785b707b340ed6c7"),
      "latitude" : 12.36547,
      "longitude" : 1.235689,
      "place_name" : "some_place",
      "city" : "City1"
}
{
      "_id" : ObjectId("544a2147785b707b340ed6c7"),
      "latitude" : 12.36547,
      "longitude" : 1.235689,
      "place_name" : "some_place",
      "city" : "City3"
}
{
      "_id" : ObjectId("544a2147785b707b340ed6c7"),
      "latitude" : 12.36547,
      "longitude" : 1.235689,
      "place_name" : "some_place",
      "city" : "City1"
 }
 {
      "_id" : ObjectId("544a2147785b707b340ed6c7"),
      "latitude" : 12.36547,
      "longitude" : 1.235689,
      "place_name" : "some_place",
      "city" : "City2"
}
{
      "_id" : ObjectId("544a2147785b707b340ed6c7"),
      "latitude" : 12.36547,
      "longitude" : 1.235689,
      "place_name" : "some_place",
      "city" : "City2"
}

How can I group the data with same city? Meaning I need array of json result first array should contain all the data which having city1 second array conatins all the data which having city2 and so on

答案1

得分: 2

你可以在Mongo中更容易地完成这个操作,使用聚合框架。

db.yourCollection.aggregate([
  {
    $group: {
      _id: "$city",
      details: {
        $push: {
          latitude: "$latitude",
          longitude: "$longitude",
          place_name: "$place_name"
        }
      }
    }
  }
])

我认为这应该可以工作,但我目前无法在工作中实际尝试。希望对你有帮助!

英文:

You could do it in Mongo itself more easily in my opinion. Use the aggregation framework.

db.yourCollection.aggregate([{$group:{_id: "$city", details:{$push: {latitude:  "$latitude",  longitude: "$longitude", place_name:"$place_name"}}}}])

I think this should work, I can't actually try it at the moment at work. hope it helps!

答案2

得分: 1

如果你正在使用mgo,简单的排序就可以解决问题。在这里,我们首先按城市排序,然后按名称排序:

query1 := collection.Find(nil).Sort("city", "place_name")
英文:

If you are using mgo, a simple sort should do the trick. Here we search first by city and then by name:

query1 := collection.Find(nil).Sort("city", "place_name")

huangapple
  • 本文由 发表于 2014年11月5日 18:25:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/26754794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定