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