使用ISODate导入CSV文件MongoDB [英] importing CSV file MongoDB with ISODate
问题描述
当我从mongoDB导出数据时,我获得以下文件:
When I export my data from mongoDB I obtain the following file:
除了日期是ISODate之外,所有内容都是mongoDB中的字符串.
Everything is a string in the mongoDB except for the date that is ISODate.
123@123.com,sha1:64000:18:BTJnM903gIt5FNlSsZIRx1tLC9ErPJuB:9YVs800sgRPr1aaLj73qqnJ6,123,123,123@123.com,2017-04-28T09:20:07.480Z,cus_AYcVXIUf68nT52
如果我将此文件导入MongoDB,它将每个值作为String值导入.我需要将日期解析为日期格式,其余的可以是字符串.
If I import this file into MongoDB it import each value as String value. I need to parse the date as Date format, the rest can be string.
我已经看到MongoImport --columnsHaveTypes有一个参数.我尝试了一下却没有任何结果:
I've seen that there's an argument for MongoImport --columnsHaveTypes. I've tryed it without any result:
mongoimport -u test-p test --authenticationDatabase test -h localhost:30158 --db test--collection users --type csv --file users.csv --upsert --upsertFields username --fields username.string\(\),password.string\(\),cname.string\(\),sname.string\(\),mail.string\(\),creation.date\(\),validation.auto\(\),clients.string\(\),customer.string\(\) --columnsHaveTypes
我收到此错误:
Failed: type coercion failure in document #0 for column 'creation', could not parse token '2017-04-28T09:20:07.480Z' to type date
我能做什么?
亲切的问候.
推荐答案
摘要:您需要提供显示日期的格式.
Summary: you need to provide the format in which the date will be presented.
从 columnsHaveTypes参数的mongoimport文档中,您不能只说created.date\(\)
-您需要提供一个参数,这是CSV中日期表示方式的模板.显然,按照转到语言time.Parse函数的方式,以您选择的格式呈现特定的参考日期.
From the mongoimport documentation on the columnsHaveTypes parameter, you can't just say created.date\(\)
- you need to provide an argument, which is a template for the way the date is represented in the CSV. Apparently the way you do this is, in accordance with the Go Language time.Parse function, by rendering a particular reference date in the format of your choice.
我认为参考日期应采用以下格式:
I think the reference date should be formatted like this:
2006-01-02T15:04:05.000Z
因此,您需要更改mongoimport调用以使用格式模板指定日期,例如:
So you need to change your mongoimport call to specify the date with the format template, like this:
creation.date\(2006-01-02T15:04:05.000Z\)
这篇关于使用ISODate导入CSV文件MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!