Tuesday, 30 May 2017

geonear sort data in node.js

I am using geonear in my node.js program using mongoose schema to find the nearest location data. It's working fine but now I want to sort among the matched data .My code is :-

BusinessDetail.find({
          geometry: {
            $nearSphere: [parseFloat(data[0].longitude),parseFloat(data[0].latitude)],
            $maxDistance: 50 / 6371
          }
        }, function(err, business) {
           console.log(business);
        }
   })

And my schema is:-

var mongoose = require('mongoose'),
  Schema = mongoose.Schema;
//=============================
// Schema
//=============================
var schema = new Schema({
  vendor_id: { type: Schema.Types.ObjectId, ref: 'Vendor' },
  category: { type: Schema.Types.ObjectId, ref: 'Category' },
  name: { type: String, required: 'Business Name is required' },
  contact: {
    contact_name: { type: String, required: 'Name is required' },
    email: { type: String, required: 'Email is required' },
    s_email: String,
    mobile: { type: String, required: 'Mobile No. is required' },
    s_mobile: String,
  },
  images: [{
    title: String,
    desc: String,
    key_large: String,
    url_large: String,
    key_thumb: String,
    url_thumb: String
  }],
  cover_image: String,
  description: String,
  seat_capacity: Number,
  float_capacity: Number,
  address: {
    address1: String,
    city: String,
    state: String,
    country: String,
    pin: Number
  },
  geometry: {
    type: [Number],
    index: "2dsphere"
  },
  events: [{ type: Schema.Types.ObjectId, ref: 'Event' }],
  highlights: [{ type: String }],
  advanced_percentage: Number,
  taxes: {
    food: Number,
    other: Number
  },
  cancellable: Boolean,
  time_slots: [{
    text: String,
    slot: String
  }],
  min_Order_quantity: String,
  outside_catering: Boolean,
  outside_decorators: Boolean,
  alcohol_allowed: Boolean,
  terms: String,
  aminities: [{ type: Schema.Types.ObjectId, ref: 'Aminity' }],
  sub_categories: [{ type: Schema.Types.ObjectId, ref: 'Category.sub_categories' }],
  status: { type: String, default: 'draft' },
  verifiedBy: { type: Schema.Types.ObjectId, ref: 'SuperAdmin' },
  verifyTime: { type: Date },
  isAvailable: Boolean,
  isBlocked: { type: Boolean, default: false },
  blockedBy: { type: Schema.Types.ObjectId, ref: 'SuperAdmin' },
  blockReason: String,
  created_at: { type: Date, default: Date.now() },
  updated_at: { type: Date },
  version: { type: Number }
});

schema.index({ geometry: '2dsphere' });
// enables beautifying 
module.exports = mongoose.model('BusinessDetail', schema);

It display all nearest data within 50km.But I can not understand how to sort the result data by the nearest location/distance from the given geolocation.



via Santu Nandi

No comments:

Post a Comment