Mongodb $ geowithin with false

时间:2014-08-01 14:21:39

标签: mongodb mongodb-query

如何使用false指定$ geoWithin运算符,它返回不匹配的文档?

.find({"loc" : { "$geoWithin" : { "$geometry" : { "type" : "Polygon" , "coordinates" : [[[40.125246,-74.327963],[40.125246,-74.325989],[40.123738,-74.325989],[40.123738,-74.327963],[40.125246,-74.327963]]]}}}})

我希望我能做到

.find({{"loc" : { "$geoWithin" : { "$geometry" : { "type" : "Polygon" , "coordinates" : [[[40.125246,-74.327963],[40.125246,-74.325989],[40.123738,-74.325989],[40.123738,-74.327963],[40.125246,-74.327963]]]}}}}:false})

但这不起作用

1 个答案:

答案 0 :(得分:0)

与MongoDB 2.6一样,不支持否定地理空间查询。

但是,您可以使用两个查询来实现结果。以下是mongo shell中的示例:

// Find all matching documents
var withinDocs = db.mycollection.find({"loc" : { "$geoWithin" : { ... }}).toArray()

// Convert results into list of _ids for query
var withinIDs = [];
withinDocs.forEach(function (d) {
    withinIDs.push(d._id);
})

// Find documents that do *not* match the geo query using $nin
db.mycollection.find({_id:{$nin:withinIDs}})