1

我在 MongoDB 中对我的用户模型执行查询,并希望首先按距离(以公里为单位)对搜索结果进行排序(使用$geoNear运算符),然后按上次登录时间排序。问题是,距离属性在浮点变量中以米的形式返回,这太准确了

这意味着我的结果是这样排序的:

+----------------+------------------------+
|    distance    |  lastLoggedInTime      |
+----------------+------------------------+
|    1700        |  16 hours ago          |
|    1700.1      |  4 hours ago           |
|    2220        |  3 months ago          |
|    2220.5      |  1 day ago             |
+----------------+------------------------+

那些相同的值,在哪里km = Math.ceil(distance / 1000)会这样排序:

+----------------+------------------------+
|    km          |  lastLoggedInTime      |
+----------------+------------------------+
|    2           |  4 hours ago           |
|    2           |  16 hours ago          |
|    3           |  1 day ago             |
|    3           |  3 months ago          |
+----------------+------------------------+
这就是我想要的。我使这些值更具可读性,以突出我面临的问题。实际值看起来更像这样:

{
    distance: 2234.3715776698273,
    lastLoggedInTime: '2019-07-13T02:14:30.173Z'
}

我试过distance自己改变属性:

      user.distance = Math.ceil(user.travel.distance / 1000);

但由于distance是由$geoNear搜索查询本身内部添加的,我只能在查询运行后访问它。因此,当我可以访问它时,结果已经不受欢迎了。

还有一个distanceMultiplier属性,我可以像这样附加到我的查询中:

      $geoNear: {
        spherical: true,
        includeLocs: 'loc',
        distanceField: 'distance',
        distanceMultiplier: 0.001,        
        near: {
          type: 'Point',
          coordinates: [lng, lat]
        },
        ...
      }

但是结果还是作为浮点数返回,(2234.3715776698273变成2.2343715776698273),也有同样的问题。我还有哪些其他选择?

4

1 回答 1

0

尝试将操作转换为聚合,然后使用$project$sort创建并按截断距离字段排序。$project 阶段将使用$trunc来提供简化的距离字段。

例如:

db.coll.aggregate([
  { "$geoNear" : {
    {
        spherical: true,
        includeLocs: 'loc',
        distanceField: 'distance',
        near: {
          type: 'Point',
          coordinates: [lng, lat]
        },
        ...
     }
  },
  {
    "$project": {
      "truncated_distance" : { "trunc" : [ "$distance", 0 ] }
    }
  },
  {
    "$sort" : {
      "truncated_distance" : 1, 
      "lastLoggedInTime": 1
    }
  }
])
于 2019-07-19T03:03:20.617 回答