如何获取其半径内包含给定位置的记录? Mongodb
How to get the records which contains the given location in its radius? Mongodb
我的 DB Mongoose 架构如下所示。
var blogSchema = mongoose.Schema({
blogText: { type: String},
location: {type: [Number]},
radius : {type: Number},
});
blogSchema.index({location: 2dsphere});
... .. .. ...
示例数据如下所示
{
"_id" : 58ef3f1919f99e3bb37ce21b,
"location" : [
77.706106,
12.9447252
],
"radius" : "4000",
"blogText" : "News 1 ....."},
{
"_id" : 58ef3f19b37ce21b19f99e3b,
"location" : [
77.709979,
12.9703882
],
"radius" : "1000",
"blogText" : "Health 1 ....."},
{
"_id" : 58ef3f1919f99e3bb37ce21b,
"location" :[
77.7060119,
12.9488936
],
"radius" : "3500",
"blogText" : "Lifestyle 1 ....."},
{
"_id" : 58ef3f1919f99e3bb37ce21b',
"location" : [
77.709979,
12.9703882
],
"radius" : "100",
"blogText" : "Sports....."},
{
"_id" : 58ef3f1919f99e3bb37ce21b',
"location" : [
77.706106,
12.9447252
],
"radius" : "800",
"blogText" : "Tech ....."}
我必须只获取那些在博客位置和半径下包含用户位置的博客。
我的查询看起来像。其中根据请求的位置和半径获取数据。
db.blogs.find({location: {$geoNear: {$geometry: {type: "Point",coordinates: [77.706106,12.9447252]}$maxDistance: 500,$minDistance: 0}}}} })
提前致谢
据此修改 answer,这可能适合您。
使用聚合通过具有三个阶段的管道推送文档。
1) $geoNear
:找到您想要的坐标附近的所有文档,并且 return 生成一个名为 distance
.
的 distanceField
2) $project
: 使用第 1 阶段中找到的 $distance
并从 $radius
中减去,存储为增量。
3)$match
:过滤掉所有小于等于0的delta。
db.blogs.aggregate([
{
'$geoNear' : {
near : [77.706106,12.9447252],
distanceField : 'distance',
includeLocs: 'location',
spherical : true
}
},
{
'$project' : {
name : 1,
location : 1,
delta : {
'$subtract' : [
'$radius',
'$distance'
]
}
}
},
{
'$match' : {
delta : { '$gte' : 0 }
}
}
])
我的 DB Mongoose 架构如下所示。
var blogSchema = mongoose.Schema({
blogText: { type: String},
location: {type: [Number]},
radius : {type: Number},
});
blogSchema.index({location: 2dsphere});
... .. .. ...
示例数据如下所示
{
"_id" : 58ef3f1919f99e3bb37ce21b,
"location" : [
77.706106,
12.9447252
],
"radius" : "4000",
"blogText" : "News 1 ....."},
{
"_id" : 58ef3f19b37ce21b19f99e3b,
"location" : [
77.709979,
12.9703882
],
"radius" : "1000",
"blogText" : "Health 1 ....."},
{
"_id" : 58ef3f1919f99e3bb37ce21b,
"location" :[
77.7060119,
12.9488936
],
"radius" : "3500",
"blogText" : "Lifestyle 1 ....."},
{
"_id" : 58ef3f1919f99e3bb37ce21b',
"location" : [
77.709979,
12.9703882
],
"radius" : "100",
"blogText" : "Sports....."},
{
"_id" : 58ef3f1919f99e3bb37ce21b',
"location" : [
77.706106,
12.9447252
],
"radius" : "800",
"blogText" : "Tech ....."}
我必须只获取那些在博客位置和半径下包含用户位置的博客。
我的查询看起来像。其中根据请求的位置和半径获取数据。
db.blogs.find({location: {$geoNear: {$geometry: {type: "Point",coordinates: [77.706106,12.9447252]}$maxDistance: 500,$minDistance: 0}}}} })
提前致谢
据此修改 answer,这可能适合您。
使用聚合通过具有三个阶段的管道推送文档。
1) $geoNear
:找到您想要的坐标附近的所有文档,并且 return 生成一个名为 distance
.
distanceField
2) $project
: 使用第 1 阶段中找到的 $distance
并从 $radius
中减去,存储为增量。
3)$match
:过滤掉所有小于等于0的delta。
db.blogs.aggregate([
{
'$geoNear' : {
near : [77.706106,12.9447252],
distanceField : 'distance',
includeLocs: 'location',
spherical : true
}
},
{
'$project' : {
name : 1,
location : 1,
delta : {
'$subtract' : [
'$radius',
'$distance'
]
}
}
},
{
'$match' : {
delta : { '$gte' : 0 }
}
}
])