Is it possible to have a 2dsphere index without requiring a location field?

771 views
Skip to first unread message

Nik Macintosh

unread,
Jun 10, 2013, 3:16:24 PM6/10/13
to mongoo...@googlegroups.com

I have a mongoose schema and model defined as follows:

var mongoose = require('mongoose')
  , Schema = new mongoose.Schema({
      email: {
        index: {
          sparse: true,
          unique: true
        },
        lowercase: true,
        required: true,
        trim: true,
        type: String
      },
      location: {
        index: { sparse: true, type: '2dsphere' },
        type: [Number]
      }
    })
  , User = module.exports = mongoose.model('User', Schema);

If I attempt:

var user = new User({ email: 'us...@example.com' });

user.save(function(err) {
  if (err) return done(err);

  should.not.exist(err);
  done();
});

I receive the error message:

MongoError: Can't extract geo keys from object, malformed geometry?:{}

Despite the location field in this schema not being required, it seems to be acting as such anyways. I have tried adding default: [0,0] which does circumvent this error, however it seems like a bit of a hack, as this is clearly not a good default, and ideally the schema would not require the user to have a location at all times.

Do geospatial indexes with MongoDB / mongoose imply that the field being indexed is required?

On StackOverflow: http://stackoverflow.com/questions/16388836/does-applying-a-2dsphere-index-on-a-mongoose-schema-force-the-location-field-to

Aaron Heckmann

unread,
Jun 26, 2013, 3:32:31 PM6/26/13
to mongoo...@googlegroups.com
answered on Stack Overflow


--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Aaron


Reply all
Reply to author
Forward
0 new messages