Mongoose: Geospatial query insert

Created on 14 Dec 2017  路  3Comments  路  Source: Automattic/mongoose

Do you want to request a feature or report a bug?

possible bug

What is the current behavior?

can't save coordinates in geospatial

Sorry to come here, but I can't find a solution.

This is my schema:

var mongoose     = require('mongoose');
var Schema       = mongoose.Schema;

var userSchema   = new Schema({
  email: {
      type: String,
      required: true,
      unique: true
    },
    password: {type: String, required: true},
    name: {type: String, required: true},
    type: {type: Boolean, default: false, required: true},
    geo: {
      type: { type: String, enum: "Point", default: "Point" },
      coordinates: { type: [ Number ],   default: [ 0,0 ] }
    }
});

userSchema.index({geo: '2dsphere'})
const User = mongoose.model('User', userSchema);
module.exports = User

I'm trying insert a new record:

db.users.insert({email: '[email protected]', password: 'tebrwrw', name: 'teste', type: true, geo: { coordinates: [ 40, 5 ]} })

and I'm getting this error:

WriteResult({
        "nInserted" : 0,
        "writeError" : {
                "code" : 16755,
                "errmsg" : "Can't extract geo keys: { _id: ObjectId('5a31d79f369885e770a989b7'), email: \"[email protected]\", password: \"tebrwrw\", name: \"teste\", type: true, geo: { coordinates: [ 40.0, 5.0 ] } }  unknown GeoJSON type: { coordinates: [ 40.0, 5.0 ] }"
        }
})

Please mention your node.js, mongoose and MongoDB version.
lastest

Most helpful comment

Here is how I do it, hope this help

    // in the schma declaration
    'loc': { type: { type: String, enum: "Point", default: "Point" }, coordinates: { type: [Number], default: [0,0] } }

    // after schema declaration*
    schema.index( { loc: "2dsphere" } );

    // and then, how I create it
    model.loc = { type: "Point", coordinates: [newarena.lng, newarena.lat] };

All 3 comments

Quoting from the referenced contributing guidelines when creating a new issue:

If you have a question about Mongoose (not a bug report) please post it to either StackOverflow, or on Gitter

You are more likely to get a response much sooner on either of those while not adding to a backlog of bugs/features that the maintainers need to sift through here.

Here is how I do it, hope this help

    // in the schma declaration
    'loc': { type: { type: String, enum: "Point", default: "Point" }, coordinates: { type: [Number], default: [0,0] } }

    // after schema declaration*
    schema.index( { loc: "2dsphere" } );

    // and then, how I create it
    model.loc = { type: "Point", coordinates: [newarena.lng, newarena.lat] };

@Inateno thanks... is it!!

Was this page helpful?
0 / 5 - 0 ratings