将 $geoWithin 与另一个查询的结果一起使用

Using $geoWithin with the result from another query

尝试使用 Mongo 数据库的地理空间查询,我的文档如下: 位置文档:

{
    "_id": ObjectId("57062a9253e564e0d522f001"),
    "geofences": [
        {
            "geofenceId": "geoFence1",
            "loc": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            -11900093200000002,
                            33.53589500000004
                        ],
                        [
                            -118.96275899999999,
                            33.48520500000012
                        ],
                        [
                            -118.98051199999996,
                            33.42859300000009
                        ],
                        [
                            -119.05307099999987,
                            33.411024
                        ],
                        [
                            -119.094128,
                            33.426871999999946
                        ],
                        [
                            -119.10887899999999,
                            33.451151000000095
                        ],
                        [
                            -119.10721499999987,
                            33.50002300000011
                        ],
                        [
                            -119.11894899999999,
                            33.500022
                        ],
                        [
                            -119.10871799999988,
                            33.519264000000135
                        ],
                        [
                            -119.07014300000003,
                            33.536415999999974
                        ],
                        [
                            -119.00093200000002,
                            33.53589500000004
                        ]
                    ]
                ]
            }
        }
    ]
}

资产文档:

{
    "_id": ObjectId("536b150c30048054e3480789"),
    "loc": {
        "type": "Point",
        "coordinates": [
            -120.954436,
            37.625814000000005
        ]
    },
    "name": "kingize",
    "type": "priceless",
    "code": "pwd"
}

它们可以正确插入,但是当我尝试执行以下操作时:

var myloc = db.locations.find().next()
 db.assetlocation.find( { loc : { $geoWithin : { $geometry : myloc.geofences } } })

我得到:

error: {
    "waitedMS" : NumberLong(0),
    "ok" : 0,
    "errmsg" : "Point must only contain numeric elements",
    "code" : 2
}

我认为这个错误只会出现在插入语句中,但令我惊讶的是它们运行得非常完美。

问题是 "locations" 集合中包含的文档中的 "geofences" 属性 实际上是一个 "array"。所以这种直接形式的数据不适合与 $geoWithin.

一起使用

为了向 $geoWithin 提供 "loc" 数据,您需要实际迭代 "geofences" 的每个数组元素并传入每个元素的数据:

var results = [];
var cursor = db.locations.find();

// Iterate the cursor
while ( cursor.hasNext() ) {
   var doc = cursor.next();

   // Iterate each geofences element
   doc.geofences.forEach(function(fence) {
       // Query to array of results
       var assets = db.assetlocation.find({ 
           "loc": { 
               "$geoWitin": {
                   "$geometry": fence.loc
               }
           }
       }).toArray();
       // Append to the total results
       results = results.concat(assets);
   });
}

// Print all results
printjson(results);

因此您需要获取每个结果文档 "first" 中的每个数组元素,然后您可以使用包含的数据提供给 "assetlocation" 集合上的查询并收集结果获得。

您向查询传递了错误的参数。不要传递 geofences 对象,而是传递包含实际坐标和几何类型的 geofences[0].loc 字段。

由于 geofenches 是一个数组,您可以循环获取单个几何图形。

尝试以下代码片段,它应该会根据选定的条件获取文档。

var myloc = db.locations.find().next()

var coor = myloc.geofences[0].loc;

db.assets.find( { loc : { $geoWithin : { $geometry : corr } } })