聚合查询中关于$ geoNear的歧义 [英] Ambiguity about $geoNear in aggregate query

查看:113
本文介绍了聚合查询中关于$ geoNear的歧义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个使用GeoJSON的基于位置的项目.我用过$ geoNear,我对此非常熟悉.现在,我需要从文档列表中检查它们是否属于maxDistance. 而这里的挑战是maxDistance尚未预定义.它随文档的不同而不同,并存储在文档中.

I'm setting up a location based project where I'm working with GeoJSON. I've used $geoNear and I'm pretty familiar with it. Now I've reached a situation where I need to check from a list of documents that whether they fall in the maxDistance or not. And the challenge here is maxDistance is not predefined. It varies from document to document and being stored in the document.

这就像要显示在地图上的每个商店,由用户设置以在用户处于所告知的商店距离范围内时被通知.和用户定义距离. 例如:用户添加位置并为其描述距离.现在,如果用例到达该位置的定义距离,该用例将在地图上通知用户.

This is something like every store to be shown up in the map that is setup by the user to be informed when the user is in range of the told distance of store. And user defines the distance. For example: User adds a location and describe a distance for it. Now the usecase will be notify user on map if he reaches in the defined distance for that location.

Location.aggregate([
{
    $geoNear: {
    spherical: true,
    near: { type: 'Point', coordinates: [ user.location.coordinates[0], user.location.coordinates[1] ] },
    maxDistance: {Needs to be taken from each Location document and each document should be filtered against own distance},
    distanceField: 'dist.calculated'
   }
 }

最糟糕的方法是我将所有文档都带一定距离,然后循环浏览以检查它们是否落在特定距离内,但是我想遵循一种良好且专业的方法.有什么线索吗?

The worst approach is I load all the documents with distance and then loop through to check if they fall in their specific distance but I want to follow a good and professional approach. Any clues?

这是位置的架构:

Location = new Schema({
  location: {
        type: {
          type: String,
          enum: ['Point'], 
        },
        coordinates: {
          type: [Number]
        }
    },
  distance: {
    type: Number
  }
})

我只是将用户的当前坐标传递给查询.

And I'm simply passing user's current coordinates to the query.

推荐答案

您可以使用

这篇关于聚合查询中关于$ geoNear的歧义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆