问题
I am working on mongodb Geospatial feature.
Following is my db schema
{
_id:5c6fea5aa7f54038cc63db92,
location:{
coordinates:[-96.81916,33.07516],
type:"Point"
},
userId:1
}
Indexing on location field with 2dsphere
I have 2 point and each one have latitude and longitude and its example is below
sw_latitude: 32.76286484066411 sw_longitude: -96.91870791625979
ne_latitude: 32.789133220783015 ne_longitude: -96.67529208374026
base on these 2 points i need to get all userId which are cover in these 2 points square
Can you please suggest me mongo query which give me result?
回答1:
I tried many way and finally i got one solution
Step for solution 1:
Step 1: find boundary from that 2 point
latitude:[32.740776 TO 32.8112099] AND longitude:[-96.910983 TO -96.683016]
Step 2: use $box from Geospatial (ref: $box from mongo) and pass this boundary point in that..
{ location: {
$geoWithin: {
$box: [ [ 32.740776, 32.8112099 ], [ -96.910983, -96.683016 ] ]
}
}
}
Step for solution 2:
Step 1: setup existing point proper way as following
latitude:[32.76286484066411 TO 32.789133220783015] AND longitude:[-96.91870791625979 TO -96.67529208374026]
Step 2: use $box from Geospatial (ref: $box from mongo) and pass this boundary point in that..
{ location: {
$geoWithin: {
$box: [ [ 32.76286484066411, 32.789133220783015 ], [ -96.91870791625979, -96.67529208374026 ] ]
}
}
}
Hope will be helpful for you
来源:https://stackoverflow.com/questions/54919308/to-find-square-area-data-from-mongodb-query-using-2-google-map-point-latitude-a