person.js 704 B

123456789101112131415161718192021222324252627
  1. // import the necessary modules
  2. var mongoose = require('../../lib');
  3. var Schema = mongoose.Schema;
  4. // create an export function to encapsulate the model creation
  5. module.exports = function() {
  6. // define schema
  7. var PersonSchema = new Schema({
  8. name: String,
  9. age: Number,
  10. birthday: Date,
  11. gender: String,
  12. likes: [String],
  13. // define the geospatial field
  14. loc: {type: [Number], index: '2d'}
  15. });
  16. // define a method to find the closest person
  17. PersonSchema.methods.findClosest = function(cb) {
  18. return this.model('Person').find({
  19. loc: {$nearSphere: this.loc},
  20. name: {$ne: this.name}
  21. }).limit(1).exec(cb);
  22. };
  23. mongoose.model('Person', PersonSchema);
  24. };