简体   繁体   中英

MongoDB error “can't extract geo keys from object, malformed geometry?”

I have this code:

db.Vehicles.ensureIndex({"location":"2dsphere"});

db.vehicles.find({
    "location": {
    $near: {
        $geometry: {
            type: "Point",
            coordinates: [32.081473, 32.081473]
        },
        $maxDistance: 5000
    }
    }
})

My location is:

location":{"type":"point", "coordinates": ["32.081473", "32.08473"]}

I am getting an error:

err: can't extract gel keys from object, malformed geometry?

I have seen some problems that looks like mine, but their solutions was adding the ensureIndex which I have added already.

We can see that your coordinates are Strings: ["32.081473", "32.08473"] whereas they should be numbers. In the schema definition, the coordinates must be defined as an array of Numbers:

var VehicleSchema = new Schema({
  loc: {
    type: {
      type: String
    },
    coordinates: [Number]
  },
});

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