mongodbと位置情報 ~地理空間インデックスの紹介
TRANSCRIPT
db.shopinfo.ensureIndex({
loc : "2d"
});
db.shopinfo.ensureIndex({
loc : "2d",
category: 1
});
var lon = 139;
var lat = 35;
db.shopinfo.save({
name: '喫茶もんご',
category: ['喫茶店'],
loc: [lon, lat]
});
db.shopinfo.find({
loc: [139, 35]
});
db.shopinfo.find({
loc : {
$near : [139.4, 35.4]
}
});
db.shopinfo.find({
loc : {
$near : [139.4, 35.4],
$maxDistance : 2
}
});
db.shopinfo.find({
loc : {
$near : [139.4, 35.4],
$maxDistance : 2
},
category: '喫茶店'
});
db.runCommand({
geoNear: "shopinfo",
near: [139.4, 35.4],
maxDistance: 2,
query: {category: '喫茶店'}
});
{
"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
}
db.shopinfo.find({
loc : {
$within : {
$box : [[130, 30], [140, 35]]
}
}
});
db.shopinfo.find({
loc : {
$within : {
$center : [[135, 35], 4]
}
}
});
db.shopinfo.find({
loc : {
$nearSphere : [139.4, 35.4]
}
});
db.shopinfo.find({
loc : {
$within : {
$centerSphere : [[135, 35], 0.1]
}
}
});
1 = 6371 km
db.runCommand({
geoNear: "shopinfo",
near: [135, 35],
query: {category: '喫茶店'},
spherical: true
});
"results" : [{
"dis" : 0.05718377899700883,
"obj" : {
"loc" : [139, 35]
}
}],