【问题标题】:Mongoimport location from independent latitude and longitude columns in a CSV来自 CSV 中独立纬度和经度列的 Mongoimport 位置
【发布时间】:2012-03-30 01:31:47
【问题描述】:

我有一个包含 3 个字段的 CSV:名称、纬度、经度。一排看起来像这样:

Place 1,73.992964,40.739037

将经纬度 mongoimport 到 loc 字段的正确方法是什么?我知道位置索引字段需要是经度,纬度并且是单个数组而不是纬度和经度的 2 个离散字段,但是如果有一种方法可以通过 mongoimport 处理从离散值到数组的处理,我将丢失

我是否需要先转换为包含经度和纬度的单列 loc 的 CSV?

Place1,[-73.992964,40.739037]

我经常要处理将纬度和经度存储在独立列中的 CSV,因此我希望找到一种使用 mongoimport 的方法。

【问题讨论】:

    标签: mongodb geospatial mongoimport


    【解决方案1】:

    Mongoimport 的功能非常有限,在这种情况下,官方建议编写一个自定义脚本,逐行解析您的 csv 文件并按照您希望它们表示的方式创建文档。

    为了创建地理空间索引,位置信息必须存储在相同的键下,如地理空间索引文档顶部的“一些示例:”部分所述:http://www.mongodb.org/display/DOCS/Geospatial+Indexing

    直接从 .csv 文件导入数据会创建如下文档:

    doc1.csv:
    place, lat, lon
    Place 1,73.992964,40.739037
    
    $ ./mongoimport -d test -c a --type csv --headerline --file doc1.csv 
    
    > db.a.find()
    { "_id" : ObjectId("4f7602d70c873ff911798fd3"), "place" : "Place 1", "lat" : 73.992964, "lon" : 40.739037 }
    

    很遗憾,无法在上述文档上创建地理空间索引。

    通过实验,我尝试导入包含您描述的第二种格式的数据的 .csv 文件,但没有成功。

    doc2.csv:
    place, loc
    Place1,[-73.992964,40.739037]
    
    $ ./mongoimport -d test -c b --type csv --headerline --file doc2.csv 
    
    > db.b.find()
    { "_id" : ObjectId("4f7602e40c873ff911798fd4"), "place" : "Place1", "loc" : "[-73.992964", "field2" : "40.739037]" }
    

    作为进一步的实验,我将 .csv 文档更改为 json 格式,并将其导入,它似乎可以工作。

    doc3.json:
    {name:"Place1" , loc:[-73.992964,40.739037]}
    
    $ ./mongoimport -d test -c c --type json --file doc3.json 
    
    > db.c.find()
    { "_id" : ObjectId("4f7604570c873ff911798fd5"), "name" : "Place1", "loc" : [ -73.992964, 40.739037 ] }
    

    但是,如果您正在编写一个脚本来将所有 .csv 文件转换为 .json 格式,那么您最好编写一个自定义脚本来将您的 .csv 文件直接导入到您的集合中。

    【讨论】:

    • 非常感谢您进行如此详细的检查。预处理是我前进的方向,但我只是想确保我没有遗漏 mongoimport 中的某些内容。
    【解决方案2】:

    我遇到了类似的问题,我通过使用sed 执行简短的预处理将 CSV 转换为合适的 JSON 格式(也使用新的GeoJSON objects)解决了这个问题:

    sed 's/\([^,]*\),\([0-9.-]*\),\([0-9.-]*\)/{ place: \1, location:{ type: "Point", coordinates: [ \3, \2 ] } }/' <data.csv >data.json
    

    对正在发生的事情的解释:

    sed          // Execute the sed command
    's/          // Use substitute mode
    
    \([^,]*\)    // Match a string containing anything except a ',' [1]
    ,            // Match a single ',' (the separator)
    \([0-9.-]*\) // Match any combination of numbers, '.' or '-' [2]
    ,            // Match a single ',' (the separator)
    \([0-9.-]*\) // Match any combination of numbers, '.' or '-' [3]
    
    /{ place: \1, location:{ type: "Point", coordinates: [ \3, \2 ] } }/'
    // Replace the match with the appropriate JSON format, inserting
    // parts of the matched pattern ([1],[2],[3])
    
    <data.csv    // Perform the command on the contents of the data.csv file
    >data.json   // Output the results to a data.json file
    

    我发现 sed 非常高效,即使使用包含约 800 万行的 csv 文件,执行此转换也只需要大约一分钟。

    然后使用mongoimport 导入新创建的 JSON 文件是一项简单的任务,如 Marc 的回答所示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-08
      • 1970-01-01
      • 1970-01-01
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-22
      相关资源
      最近更新 更多