简体   繁体   中英

Error message when using geospatial indexing via Mongoose

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 = CheckinSchema

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

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