如何在 mongoDB 中存储地理空间信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15274834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to store geospatial information in mongoDB
提问by José Luis
Acording to the mongoDB documentation (link) if you want to store geospatial information in a document field, you have two options, an array or an embedded document, and the order should be always longitude, latitude.
根据 mongoDB 文档(链接),如果要将地理空间信息存储在文档字段中,您有两个选项,一个数组或一个嵌入文档,并且顺序应始终为经度、纬度。
If I want to use an embedded document, how can I ensure the field's order?
如果我想使用嵌入式文档,如何确保字段的顺序?
Or must the fields in the embedded document have a specific name?
或者嵌入文档中的字段必须具有特定名称?
回答by Kay
with an embedded document, regardless of the name of the field in the embedded document, the first field should contain the longitude value and the second field should contain the latitude value. For example:
对于嵌入文档,无论嵌入文档中的字段名称如何,第一个字段应包含经度值,第二个字段应包含纬度值。例如:
db.zips2.insert( { _id: 1, city: "b", loc: { x: -73.974, y: 40.764 } } )
db.zips2.insert( { _id: 2, city: "b", loc: { x: -73.981, y: 40.768 } } )
Here the xfield would be the longitude; and the yfield would be the latitude.
这里的x字段是经度;和ÿ场将是纬度。
Regards
问候
回答by Andy Refuerzo
2D Geospatial Index, Store Location Data
"All documents must store location data in the same order. If you use latitude and longitude as your coordinate system, always store longitude first. MongoDB's 2d spherical index operators only recognize [ longitude, latitude] ordering."
“所有文档都必须以相同的顺序存储位置数据。如果您使用纬度和经度作为坐标系,请始终首先存储经度。MongoDB 的 2d 球面索引运算符仅识别 [经度,纬度] 排序。”
Here's a good post about Geospatial Queries in MongoDBin case you need it.
回答by emilie zawadzki
I would advise you to name your field: x and y
, instead of longitude/latitude
, because after an update, longitude and latitude will be reordered in an alphabetical way, so inverted.
我建议您将您的字段命名为:x and y
,而不是longitude/latitude
,因为更新后,经度和纬度将按字母顺序重新排序,因此倒置。
回答by xameeramir
Geospatial indexes allow us to find things based on geographic location.
地理空间索引允许我们根据地理位置查找事物。
We've 2 options:
我们有2个选择:
- 2D
- 3D
- 二维
- 3D
In 2-D, we've a cartesian plane with x
& y
coordinates and a bunch of different objects.
在二维中,我们有一个带有x
&y
坐标和一堆不同对象的笛卡尔平面。
The document needs to store some sort of x
,y
location with ensureIndex({'location':'2d', 'type':1})
.
文档需要存储某种x
,y
位置ensureIndex({'location':'2d', 'type':1})
。
The type
(optional) option specifies the direction of the index i.e. ascending or descending. It can be a compound index.
的type
(可选)选项指定的索引即升序或降序的方向。它可以是一个复合索引。
Example usage:
用法示例:
To find nearby locations use command:
要查找附近的位置,请使用命令:
db.collectionName.find({location: {$near:[x,y]}})
In practice, the way this is often used is through a limit
. To limit the results to for example 20append .limit(20)
.
在实践中,经常使用的方式是通过limit
. 将结果限制为例如20append .limit(20)
。
db.collectionName.find({location: {$near:[x,y]}}).limit(20)
回答by Isaac Peraza Leon
MongoDB documentation said the following:
MongoDB 文档说明如下:
A field named coordinates that specifies the object's coordinates. If specifying latitude and longitude coordinates, list the longitude firstand then latitude:
一个名为坐标的字段,用于指定对象的坐标。如果指定纬度和经度坐标,首先列出经度,然后列出纬度:
Valid longitude values are between -180 and 180, both inclusive. Valid latitude values are between -90 and 90 (both inclusive).
有效的经度值介于 -180 和 180 之间,包括两者。有效的纬度值介于 -90 和 90(包括两者)之间。
Oficial MongoDB documentation: https://docs.mongodb.com/manual/geospatial-queries/#geospatial-indexes
MongoDB 官方文档:https: //docs.mongodb.com/manual/geospatial-queries/#geospatial-indexes