猫鼬按地理位置分组

时间:2020-10-25 07:00:25

标签: mongodb

所以我想知道是否可以将地理位置long和lat记录下来。

基本上,我正在尝试制作热图(而且在Reactjs中无法解决)。因此,下一个选择是将long和lat匹配(如果它们匹配),并为每个值分配一个(0-10)值,数字越大div大小越大。

但是现在我需要弄清楚如何将其分组。

location.aggregate([
            {  
              $addFields: {
                location: {
                  coordinates: {
                    $map: {
                      input: "$location.coordinates",
                      in: { $toString: "$$this" }
                    }
                  }
                }
              }
            },
            {
                $group: {"$location.coordinates"}
            },
            {
                $unset:["traits","UUID","createdAt","updatedAt"]
            },
            {$skip:0},
            { $limit: 600 }
          ])

RAW JSON

{"_id":"5ef6f03e5b43e19dabf7c705","__v":0,"location":{"coordinates":["115.862","-31.9674"]}},{"_id":"5ef6f0d55b43e19dabf7d8a1","__v":0,"location":{"coordinates":["115.862","-31.9674"]}},{"_id":"5ef6f1355b43e19dabf7e32d","__v":0,"location":{"coordinates":["115.862","-31.9674"]}},{"_id":"5ef6f2915b43e19dabf80a39","__v":0,"location":{"coordinates":["115.862","-31.9674"]}},{"_id":"5ef6f4055b43e19dabf83819","__v":0,"location":{"coordinates":["115.862","-31.9674"]}},{"_id":"5ef6f5d95b43e19dabf86d13","__v":0,"location":{"coordinates":["115.862","-31.9674"]}},

1 个答案:

答案 0 :(得分:0)

您可以尝试

  • $grouplocation.coordinates
  • $project将十进制坐标转换为location.coordinates字段中的字符串
location.aggregate([
  { $group: { _id: "$location.coordinates" } },
  {
    $project: {
      _id: 0,
      "location.coordinates": {
        $map: {
          input: "$_id",
          in: { $toString: "$$this" }
        }
      }
    }
  },
  { $skip: 0 },
  { $limit: 600 }
])

Playground