Wednesday, August 15, 2018

7. MongoDB : GEO JSON & Geospatial Index

7. MongoDB : GEO JSON & Geospatial Index








//GEO JSON DATA ########################################################


/GEO JSON - which contains geography informations. - and location: {type: "point", coordinates: [-78.97,40.77]} this kind of data should be must.

db.createCollection("places");

db.places.insert({location: {type: "Point", coordinates: [12.925397371881461,77.63657815315337]}, name:"BangalorePizza_1", category:"PizzaShop"});
db.places.insert({location: {type: "Point", coordinates: [12.915442118877891,77.63820893631903]}, name:"BangalorePizza_2", category:"PizzaShop"});
db.places.insert({location: {type: "Point", coordinates: [12.916989810315489,77.63220078798098]}, name:"BangalorePizza_3", category:"PizzaShop"});

db.places.insert({location: {type: "Point", coordinates: [12.330624740837576,76.62695926148842]}, name:"MysorePizza_1", category:"PizzaShop"});
db.places.insert({location: {type: "Point", coordinates: [12.325232832287508,76.63530303596097]}, name:"MysorePizza_2", category:"PizzaShop"});

db.places.insert({location: {type: "Point", coordinates: [12.978403513741311,77.56954207070805]}, name:"Bangalore Railway Station1", category:"RailwayStation"});
db.places.insert({location: {type: "Point", coordinates: [12.978063446690769,77.57170478534276]}, name:"Bangalore Railway Station22", category:"RailwayStation"});




//GEO JSON INDEX ########################################################

//Geospatial Index
//to query , 1st we have to create index
//compound index -> geoIndex with normalIndex - YES possible
//compound index -> multiple geoIndex - NO not possible.
//db.collection.createIndex({<location field> : "2dsphere"}) is mandatory

db.places.createIndex({location: "2dsphere", category: -1, name:1});
db.places.createIndex({category: -1, name:1, location: "2dsphere"});
db.places.createIndex({location: "2dsphere"});

db.places.getIndexes();

//GEO JSON FIND ########################################################

db.places.find(
   { location: { $geoWithin: { $center: [ [12.91740963747045,77.63365255325667], 10 ] } } }
)

db.places.find(
   {category:"PizzaShop",location: { $geoWithin: { $center: [ [12.91740963747045,77.63365255325667], 10 ] } }}
)

db.places.find({location: {$geoWithin:{$box:[[-100,74.00],[10,100]}}})
db.places.find({location: {$geoWithin:{$geometry:{type:"Polygon", coordinates:[[[0,0],[-90,45.00],[-100,-50.00],[


     








No comments:

Post a Comment