开发者

Error message when using geospatial indexing via Mongoose

开发者 https://www.devze.com 2023-04-12 07:46 出处:网络
My Schema is as follows: mongoose = require \'mongoose\' ObjectId= mongoose.Schema.ObjectId CheckinSchema = new mongoose.Schema

My Schema is as follows:

mongoose = require 'mongoose'
ObjectId  = mongoose.Schema.ObjectId

CheckinSchema = new mongoose.Schema
  text:
    type: String, required: true
  location_latLong:
    lat:
      type: Number
    lon:
      type: Number
  location_country:
    type: String
  addedOn:
    type: Date
    default: Date.now

CheckinSchema.index
  location_latLong: '2d'

exports.CheckinSchema = Checki开发者_Python百科nSchema

The Model is generated separately. I get an error however when running a query. The error is:

count fails:{ errmsg: "exception: can't find special index: 2d for: { location_latLong: { $wi...", code: 13038, ok: 0.0 }

My query is:

{ location_latLong: { '$within': { '$box': [[ '-100', '-100' ],[ '100', '100' ]] } } }

So my question is.... what gives? How can I properly do Geospatial indexing in Mongoose (using Node.js)


THis is because you have specified the wrong order in your geo index, Since mongodb is based on GeoJSON, its recommented to have the longitude field first

instead this

location_latLong:
    lat:
      type: Number
    lon:
      type: Number

use this

location_latLong:
    lon:
      type: Number
    lat:
      type: Number

The names you assign to a location object (lon,lat keys) are completely ignored, only the ordering is detected.

In mongodb geospatial page, its recommended in multiple places

By default, the index assumes you are indexing longitude/latitude and is thus configured for a [-180..180) value range

and

The code assumes that you are using decimal degrees in (longitude, latitude) order. This is the same order used for the GeoJSON spec. Using (latitude, longitude) will result in very incorrect results, but is often the ordering used elsewhere, so it is good to double-check. The names you assign to a location object (if using an object and not an array) are completely ignored, only the ordering is detected.

I have already answered this before.

Cheer

0

精彩评论

暂无评论...
验证码 换一张
取 消