Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geospatial query insert #5905

Closed
Igorpollo opened this issue Dec 14, 2017 · 3 comments
Closed

Geospatial query insert #5905

Igorpollo opened this issue Dec 14, 2017 · 3 comments

Comments

@Igorpollo
Copy link

Igorpollo commented Dec 14, 2017

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: 'test2@teste.com', 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: \"test2@teste.com\", 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

@JasonCust
Copy link

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.

@Inateno
Copy link

Inateno commented Dec 17, 2017

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] };

@Igorpollo
Copy link
Author

@Inateno thanks... is it!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants