Tore
Tore

Reputation: 1264

Mongoose query for Lat/Lng in Range

I am trying to query for data points within a given lat/lng range. Can you reference the elements of an object in a mongoose query like I have done ('location.lat') and ('location.long')? If so, I am not getting any data back from this query. I was querying for all data points, and that was working just fine - now I am simply trying to refine the query to only the points in a given range.

EDIT

Querying for points that have a new format (see the updated schema below):

var range = {"topLeft":[-113.51526849999999,53.24204911518776],"bottomRight":[-131.0933935,41.397215826886736],"topRight":[-131.0933935,53.24204911518776],"bottomLeft":[-113.51526849999999,41.397215826886736]};


db.datapoints.find({ 
    geo: {
        $geoWithin : {
            $geometry: {
                type: "Polygon",
                coordinates: [
                  [
                    range.topLeft, range.topRight, range.bottomRight, range.bottomLeft
                  ]
                ]
            }
        }
    }
})

but I am getting:

error: {
    "$err" : "Malformed geo query: { $geoWithin: { $geometry: { type: \"Polygon\", coordinates: [ [ [ -113.5152685, 53.24204911518776 ], [ -131.0933935, 53.24204911518776 ], [ -131.0933935, 41.39721582688674 ], [ -113.5152685, 41.39721582688674 ] ] ] } } }",
    "code" : 16677
}

NOTE: the range parameter looks like this:

UPDATED SCHEMA

var mongoose = require('mongoose');

var dataPointSchema = mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    geo: {type: [Number], index: '2d'},
    ...
    timestamp: {type: Date, default: Date.now}
    ...
});

Upvotes: 1

Views: 1410

Answers (1)

Tore
Tore

Reputation: 1264

As seen here:

MongoDB: "Malformed geo query" with $geoIntersect on a polygon

You must close the polygon by making the first and last points the same.

Upvotes: 1

Related Questions