Node,js - Mongoose - Unable to save Geo Polygon - CastError: Cast to number failed
Edit:It seem that we can set 2dsphere on Point only, not Polygon
so I remove index and it worked.
file: app.js
var mongoose = require('mongoose');mongoose.connect('localhost', 'geo-database');var GeoSchema = mongoose.Schema({ name: String , coordinates: []});//GeoSchema.index({ coordinates: '2dsphere' });var Geo = mongoose.model('geos', GeoSchema);Geo.on('index', function () { function cb() { console.log(arguments); } geoPoint = new Geo({ coordinates: [2,2], type: 'Point' }).save(cb); geoPolygon = new Geo({ type: 'Polygon', coordinates: [[ [0,0], [3,0], [3,3], [0,3], [0,0] ]] }).save(cb);})
terminal:
$mongo --versionMongoDB shell version: 2.5.5-pre-npm install mongoosenode app
output:
{ '0': null, '1': { __v: 0, _id: 52b6e82493d21060b3000001, coordinates: [ 2, 2 ] }, '2': 1 }{ '0': null, '1': { __v: 0, _id: 52b6e82493d21060b3000002, coordinates: [ [ [Object], [Object], [Object], [Object], [Object] ] ] }, '2': 1 }