mongoose 无法将数据从包含unix时间戳的csv文件迁移到mongodb

8iwquhpp  于 2023-01-02  发布在  Go
关注(0)|答案(1)|浏览(111)

csv示例

时间戳、名称、金额
1571967208,拉胡尔,15.7
1571967200,拉朱,25.7

代码示例

mongoimport --db=crypto --collection=t --type=csv \
   --columnsHaveTypes \
   --fields="timestamp.date(), name.string(), amount.double()" \
   --file="text.csv"

结果

Failed: type coercion failure in document #1 for column 'timestamp', could not parse token '1571967208' to type date
nzkunb0c

nzkunb0c1#

您可以使用jq将CSV预处理为MongoDB Extended JSON (v2),如下所示:

jq --null-input --raw-input 'input | split(",") as $fields | inputs | split(",") as $values |{($fields[0]): {"$date": {"$numberLong": ($values[0]+"000")}}, ($fields[1]): $values[1], ($fields[2]): ($values[2] | tonumber)}' yourFile.csv > yourFile.json

这会将示例CSV转换为:

{
  "timestamp": {
    "$date": {
      "$numberLong": "1571967208000"
    }
  },
  "name": "Rahul",
  "amount": 15.7
}
{
  "timestamp": {
    "$date": {
      "$numberLong": "1571967200000"
    }
  },
  "name": "Raju",
  "amount": 25.7
}

参见jqplay.org example
然后:

mongoimport --db=crypto --collection=t --file=yourFile.json

相关问题