【问题标题】:Export mongo db into multiple CSV将 mongo db 导出为多个 CSV
【发布时间】:2019-12-14 18:43:38
【问题描述】:

我一直在寻找一种将 mongo db 导出到多个文件中的 csv 的方法。

  • Db 包含来自不同国家/地区的用户,因此我想对用户进行分组并分别为每个国家/地区导出 CSV。

我尝试了mongoExport,但没有成功。

mongoexport 是一个生成 JSON 或 CSV 数据导出的实用程序 存储在 MongoDB 实例中。

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    您也许可以在mongoexport 中使用--query 参数;见mongoexport --query documentation page

    例如,我的收藏是:

    > db.test.find()
    { "_id": 0, "country": "US" }
    { "_id": 1, "country": "US" }
    { "_id": 2, "country": "US" }
    { "_id": 3, "country": "AU" }
    { "_id": 4, "country": "AU" }
    

    然后我们可以运行两个mongoexport 来导出每个国家/地区:

    $ mongoexport -d test -c test --type=csv --fields="_id,country" --query "{country: 'US'}"
    _id,country
    0,US
    1,US
    2,US
    
    $ mongoexport -d test -c test --type=csv --fields="_id,country" --query "{country: 'AU'}"
    _id,country
    3,AU
    4,AU
    

    您可能希望创建一个脚本,首先发现集合中的国家/地区列表,然后为每个国家/地区运行 mongoexport

    【讨论】:

      猜你喜欢
      • 2015-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-12
      • 2020-10-15
      • 1970-01-01
      相关资源
      最近更新 更多