通过mongoimport转换为日期MongoDB [英] Convert to date MongoDB via mongoimport

查看:59
本文介绍了通过mongoimport转换为日期MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以csv格式下载了大量数据.我正在使用mongoimport将数据输入到MongoDB中进行处理. 如何将日期转换成MongoDB可以识别的日期格式?

I have downloaded huge chunks of data in the format in csv. I am using mongoimport to enter the data into MongoDB for processing. How do I get the date into date format recognized by MongoDB?

带有标题的样本数据

Date, Open Price, High Price, Low Price, Last Traded Price , Close Price, Total Traded Quantity, Turnover (in Lakhs)
04-Apr-2014,901,912,889.5,896.75,892.85,207149,1867.08
03-Apr-2014,908,918,897.65,900,900.75,156260,1419.9
02-Apr-2014,916,921.85,898,900.7,900.75,175990,1591.97

推荐答案

据我所知,无法使用mongoimport做到这一点.

As far as I know, there is no way to do this with mongoimport.

但是这可以通过导入数据然后运行以下脚本来实现(请注意,像Neil的Lunn脚本中那样,用一个月来的所有哈希值是没有意义的,因为mongo可以通过执行此操作正确地转换您的日期new Date('04-Apr-2014') ):

But this is achievable by importing the data and then running the following script (note that there is no point of all this hastle with a monthes as in Neil's Lunn script, because mongo can properly convert your date by doing this new Date('04-Apr-2014')):

db.collName.find().forEach(function(el){
    el.dateField = new Date(el.dateField);
    db.collName.save(el)
});

PS 如果时区非常重要(我假设不是,如果只有日期而没有时间信息),则只需在本地计算机上更改时区,然后运行查询即可. (感谢Neil Lunn对此进行的澄清)

PS If timezone is so important (I assume that it is not, if there are only dates without time information), you can just change timezone on your local machine and then run the query. (Thanks to Neil Lunn for clarification regarding this)

这篇关于通过mongoimport转换为日期MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆