MongoDBと位置情報 ~地理空間インデックスの紹介

10,913
-1

Published on

Published in: Technology

MongoDBと位置情報 ~地理空間インデックスの紹介

  1. 1. db.shopinfo.ensureIndex({ loc : "2d"});db.shopinfo.ensureIndex({ loc : "2d", category: 1 });
  2. 2. var lon = 139;var lat = 35;db.shopinfo.save({ name: 喫茶もんご, category: [喫茶店], loc: [lon, lat]});
  3. 3. db.shopinfo.find({ loc: [139, 35]});
  4. 4. db.shopinfo.find({ loc : { $near : [139.4, 35.4] }});
  5. 5. db.shopinfo.find({ loc : { $near : [139.4, 35.4], $maxDistance : 2 }});
  6. 6. db.shopinfo.find({ loc : { $near : [139.4, 35.4], $maxDistance : 2 }, category: 喫茶店});
  7. 7. db.runCommand({ geoNear: "shopinfo", near: [139.4, 35.4], maxDistance: 2, query: {category: 喫茶店}});
  8. 8. { "ns" : "test.shopinfo", "near" : "1110100101001011000011000101000010111011111111011110", "results" : [{ "dis" : 0.5656861498877501, "obj" : { "_id" : ObjectId("4d01f5616919cb54afce6a77"), "name" : "喫茶もんご", "category" : ["喫茶店"], "loc" : [139, 35] } }], "stats" : { "time" : 0, "btreelocs" : 0, "nscanned" : 1, "objectsLoaded" : 1, "avgDistance" : 0.5656861498877501, "maxDistance" : 0.5656861498877501 }, "ok" : 1}
  9. 9. db.shopinfo.find({ loc : { $within : { $box : [[130, 30], [140, 35]] } }});db.shopinfo.find({ loc : { $within : { $center : [[135, 35], 4] } }});
  10. 10. db.shopinfo.find({ loc : { $nearSphere : [139.4, 35.4] }});db.shopinfo.find({ loc : { $within : { $centerSphere : [[135, 35], 0.1] } }});
  11. 11. 1 = 6371 km
  12. 12. db.runCommand({ geoNear: "shopinfo", near: [135, 35], query: {category: 喫茶店}, spherical: true});"results" : [{ "dis" : 0.05718377899700883, "obj" : { "loc" : [139, 35] }}],

×