Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 如何在$nearSphere查询中使用$maxDistance的文档属性?_Mongodb_Meteor_Geolocation_Mongodb Query_Aggregation Framework - Fatal编程技术网

Mongodb 如何在$nearSphere查询中使用$maxDistance的文档属性?

Mongodb 如何在$nearSphere查询中使用$maxDistance的文档属性?,mongodb,meteor,geolocation,mongodb-query,aggregation-framework,Mongodb,Meteor,Geolocation,Mongodb Query,Aggregation Framework,我有一个包含以下文档的集合: { "_id" : "cysMrqjootq6YS6WP", “profile” : { …… "deliveryDistance” : 20, "address" : { "loc" : { "type" : "Point", "coordinates" : [ —2.120

我有一个包含以下文档的集合:

{
    "_id" : "cysMrqjootq6YS6WP",
    “profile” : {
        ……
        "deliveryDistance” : 20,
        "address" : {
            "loc" : {
                "type" : "Point",
                "coordinates" : [
                    —2.120361,
                    52.536273
                ]
            }       }
    }
}
我有一个GeoJSON点,比如:

var referencePoint= {
                "type" : "Point",
                "coordinates" : [
                    —2.120361,
                    52.536273
                ]
            }
我正在使用Meteor.js、Node.js和MongoDB。我想创建一个查询,其中此点的maxDistance是我收藏中每个文档的deliveryDistance属性

如果maxDistance是固定值,则查询将为:

myCollection.find({
    “profile.address.loc":{
      "$nearSphere": {
        "$geometry": referencePoint,
        "$maxDistance": 20000 //for 20Kms
      }
    }
  })

但事实并非如此。对于每个文档,maxDistance必须是“profile.deliveryDistance”的值。如何在此查询中将文档中的此值用作maxDistance?可能吗?如果没有,还有其他想法吗?

您不能在
.find()
查询中引用文档的现有属性,至少不能在
$near
$nearSphere
操作中引用

相反,这里的方法是使用聚合框架和。这允许您计算到查询点的距离,然后比较该距离是否在文档中的“deliveryDistance”范围内

因此,对于meteor,您最好安装软件包,然后执行以下操作:

{
    "_id" : "cysMrqjootq6YS6WP",
    “profile” : {
        ……
        "deliveryDistance” : 20,
        "address" : {
            "loc" : {
                "type" : "Point",
                "coordinates" : [
                    —2.120361,
                    52.536273
                ]
            }       }
    }
}
Meteor.publish(“攻击结果”,函数(referencePoint){ var self=这个; var结果=myCollection.aggregate([ {“$geoNear”:{ “近”:参考点, “距离字段”:“距离”, “球形”:正确 }}, {“$redact”:{ “$cond”:{ “if”:{“$lt”:[“$distance”,“$profile.deliveryDistance”]}, “然后”:“$$KEEP”, “else”:“$$PRUNE” } }} ]); _.每个(结果、功能(结果){ 添加(“客户\集合\名称”,结果。\ id{ “profile”:result.profile, “距离”:result.distance }); }); self.ready(); }) 如果您的MongoDB服务器低于2.6版(对于地理空间查询,必须至少为2.4版),则您将使用
$project
$match
来过滤不在“deliveryDistance”范围内的文档:

var results=myCollection.aggregate([
{“$geoNear”:{
“近”:参考点,
“距离字段”:“距离”,
“球形”:正确
}},
{“$project”:{
"简介":一,,
“距离”:1,
“在“:{“$lt”:[“$distance”,“$profile.distance”]}
}},
{“$match”:{“within”:true}
]);
但这是最基本的情况,您向服务器提供了计算距离比较的工具,然后返回这些文档中的任何一个

聚合输出的包装实际上取决于在应用程序中使用数据的重要方式。这只是将输出放入客户机可寻址集合的一个示例


当然,您也可以深入研究驱动程序内部,以调用
.aggregate()
as,但它可能没有使用提到的meteor包灵活。

当然,聚合。非常感谢:)它也只适用于$match,不需要$project