Different semantics in $not $geoWithin with Polygon geometries between MongoDB 2.4 and 2.6 -
i have run following experiment, comparing how mongodb 2.4 , mongodb 2.6 behave regarding $geowithin
selector combined $not
polygons (i.e. "outside polygon" query). i'm including particular versions (three numbers), alghouth guess happend same other minor versions of 2.4 , 2.6.
two documents (a , b) created in given collection: p
field set coordinates [1, 1] , b without p
field. next, create 2dsphere index in p
, query area outside triangle vertices [0, 0], [0, 4]and [4, 0]. note inside polygon (so not supposed got query).
with 2.4.9:
db.x.insert({k: "a", p: [1,1]}) db.x.insert({k: "b"}) db.x.ensureindex({"p": "2dsphere"}) db.x.find({p: { $not: { $geowithin: { $geometry: { type: "polygon", coordinates: [ [ [ 0, 0 ], [ 0, 4 ], [ 4, 0 ], [ 0, 0 ] ] ] } } } }}) --> no result
makes sense: not returned (as inside polygon) , b not returned (given doesn't have p
field).
next, testing 2.6.1 same script:
db.x.insert({k: "a", p: [1,1]}) db.x.insert({k: "b"}) db.x.ensureindex({"p": "2dsphere"}) db.x.find({p: { $not: { $geowithin: { $geometry: { type: "polygon", coordinates: [ [ [ 0, 0 ], [ 0, 4 ], [ 4, 0 ], [ 0, 0 ] ] ] } } } }}) -> result: b
it seems in 2.6 semantics have changed, when 2dsphere-indexed field not in given document, document considered outside possible polygon.
changing semantics between versions ok long mechanism in new version allows configure behaviour in old way. thought mechanism using { "2dsphereindexversion" : 1 }
@ index creation time (based on read here, maybe misunderstood information...). however, result (with 2.6.1 again) same:
db.x.insert({k: "a", p: [1,1]}) db.x.insert({k: "b"}) db.x.ensureindex({"p": "2dsphere"}, { "2dsphereindexversion" : 1 }) db.x.find({p: { $not: { $geowithin: { $geometry: { type: "polygon", coordinates: [ [ [ 0, 0 ], [ 0, 4 ], [ 4, 0 ], [ 0, 0 ] ] ] } } } }}) -> result b
thus, there way of using mongodb 2.6 same semantics mongodb 2.4 in sense document without 2dsphere-indexed not returned in "outside poylgon" queries?
the query result in 2.6 right - query result in 2.4 think call incorrect. technically, query asks documents not match $geowithin
condition. "k" : "b"
document not match $geowithin
condition, should returned query. can drop results without p
field using $exists
:
db.x.find({ "p" : { "$exists" : true, "$not" : { "$geowithin" : { "$geometry" : { "type": "polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 4 ], [ 4, 0 ], [ 0, 0 ] ] ] } } } } })
also note 1) $not
query isn't using geo index, can check explain, , 2) when using 2dsphere index should store points geojson
{ "k" : "a", "p" : { "type" : "point", "coordinates" : [1,1] } }
technically it's required in mongodb >= 2.6, , docs should error not use geojson, seems work us.
Comments
Post a Comment