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

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

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

问题

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

文档格式如下:

  1. {
  2. "_id" : ObjectId("544a2147785b707b340ed6c7"),
  3. "latitude" : 12.36547,
  4. "longitude" : 1.235689,
  5. "place_name" : "some_place",
  6. "city" : "City1"
  7. }
  8. {
  9. "_id" : ObjectId("544a2147785b707b340ed6c7"),
  10. "latitude" : 12.36547,
  11. "longitude" : 1.235689,
  12. "place_name" : "some_place",
  13. "city" : "City3"
  14. }
  15. {
  16. "_id" : ObjectId("544a2147785b707b340ed6c7"),
  17. "latitude" : 12.36547,
  18. "longitude" : 1.235689,
  19. "place_name" : "some_place",
  20. "city" : "City1"
  21. }
  22. {
  23. "_id" : ObjectId("544a2147785b707b340ed6c7"),
  24. "latitude" : 12.36547,
  25. "longitude" : 1.235689,
  26. "place_name" : "some_place",
  27. "city" : "City2"
  28. }
  29. {
  30. "_id" : ObjectId("544a2147785b707b340ed6c7"),
  31. "latitude" : 12.36547,
  32. "longitude" : 1.235689,
  33. "place_name" : "some_place",
  34. "city" : "City2"
  35. }

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

英文:

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

Doc format

  1. {
  2. "_id" : ObjectId("544a2147785b707b340ed6c7"),
  3. "latitude" : 12.36547,
  4. "longitude" : 1.235689,
  5. "place_name" : "some_place",
  6. "city" : "City1"
  7. }
  8. {
  9. "_id" : ObjectId("544a2147785b707b340ed6c7"),
  10. "latitude" : 12.36547,
  11. "longitude" : 1.235689,
  12. "place_name" : "some_place",
  13. "city" : "City3"
  14. }
  15. {
  16. "_id" : ObjectId("544a2147785b707b340ed6c7"),
  17. "latitude" : 12.36547,
  18. "longitude" : 1.235689,
  19. "place_name" : "some_place",
  20. "city" : "City1"
  21. }
  22. {
  23. "_id" : ObjectId("544a2147785b707b340ed6c7"),
  24. "latitude" : 12.36547,
  25. "longitude" : 1.235689,
  26. "place_name" : "some_place",
  27. "city" : "City2"
  28. }
  29. {
  30. "_id" : ObjectId("544a2147785b707b340ed6c7"),
  31. "latitude" : 12.36547,
  32. "longitude" : 1.235689,
  33. "place_name" : "some_place",
  34. "city" : "City2"
  35. }

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中更容易地完成这个操作,使用聚合框架。

  1. db.yourCollection.aggregate([
  2. {
  3. $group: {
  4. _id: "$city",
  5. details: {
  6. $push: {
  7. latitude: "$latitude",
  8. longitude: "$longitude",
  9. place_name: "$place_name"
  10. }
  11. }
  12. }
  13. }
  14. ])

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

英文:

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

  1. 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,简单的排序就可以解决问题。在这里,我们首先按城市排序,然后按名称排序:

  1. 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:

  1. 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:

确定