简体   繁体   中英

Mongoose geospatial intersect with polygon

I've been searching for an hour or so and can't seem to find a solution to this.

I have a defined polygon in my Node.js application stored as:

[[-0.4449,51.4532],[-0.4586,51.4562],[-0.4591,51.4572],...

Where each point is given as [lng,lat] .

In my MongoDB collection I have objects with the following structure:

_id: ...
givenID: ...
location: [lng, lat]
...

And I'm representing the location property as:

  location: {
    type: [Number]
  },

in my mongoose schema.

I can query all records out and the schema maps the location correctly and I get [lng, lat] back as expected, but I can't seem to execute an intersects on any of my documents in my collection.

My query is as following:

  let geoJson ={
    "type": "Polygon",
    "coordinates": geozone
  };

  try {
    const test = await GpsLog.where('location').intersects().geometry({
      type: "Polygon",
      coordinates: geozone
    });

where geozone is the polygon listed above.

I have a 2dsphere index on the location in each document too for reference.

If I pass in the geospatial type as a LineString I get back 0 records, but passing it in as a Polygon gives me geojson coordinates must be an array , but as far as I can tell they are an array (obviously).

Is there something I've missed? I'm sure the bug is something tiny that I haven't spotted.

Silly bug, the coordinates property needed to be wrapped in another set of [ ] for it to be a valid Polygon . Works fine now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM