echoes-xyz / mongoose-geojson-schema

Schema definitions for GeoJSON types for use with Mongoose JS
MIT License
79 stars 25 forks source link

Can't use $geoIntersects #13

Closed vedi closed 7 years ago

vedi commented 8 years ago

I have:

"mongoose": "^4.4.12",

When I'm trying to do something like that:

MyModel.find({
  myLocField: {
    $geoIntersects: {
        $geometry: {
            type: 'Point' ,
            coordinates: [32.694865977875075, 35.293922424316406]
        }
    }
  }
});

I'm getting:

Unhandled rejection Error: Can't use $geoIntersects
    at SchemaType.castForQuery (/xxx/node_modules/mongoose/lib/schematype.js:875:13)
    at cast (/xxx/node_modules/mongoose/lib/cast.js:169:39)
    at Query.cast (/xxx/node_modules/mongoose/lib/query.js:2575:10)

I solved this, implementing:

GeoJSON.prototype.castForQuery = function ($cond, val) {
  if (arguments.length === 2) {
    return val;
  }
  return $cond;
}

If you think, it's good enough, you can put it to the code, and do the same for other classes.

There is a workaround, without changing the module:

  mongoose.SchemaType.prototype.$conditionalHandlers.$geoIntersects = ($cond, val) => {
    if (arguments.length === 2) {
      return val;
    }
    return $cond;
  };

But you'll need to do this for every additional condition you want to support, and you need to understand possible impact of this way.

joshkopecek commented 7 years ago

As far as I know this has been resolved by PR #12