如何从字符串转换为日期数据类型? [英] How to convert from string to date data type?

查看:31
本文介绍了如何从字符串转换为日期数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将数据从 csv 文件上传到 MongoDB.由于在使用 BI 工具创建报告时面临问题,它将 OrderDate 作为字符串数据类型.我的收藏中有大约 10000 条记录.

I am uploading data from a csv file to MongoDB. It is taking OrderDate as string data type due to which facing problems while creating reports using a BI tool. I have about 10000 records in my collection.

谁能帮助我如何通过单个查询将 OrderDate 的数据类型更改为 Date?

Could anyone help me how can I change the data Type of OrderDate to Date with a single query?

推荐答案

我不认为你可以通过单个查询来更改字段的类型.最简单的方法是在插入过程中使用 ISODate 函数将数据字符串转换为 Date 格式.但是,如果你想处理你已经插入的数据,你可以使用 mongodb 控制台使用以下代码:

I don't think that you can change field's type with single query. The easiest way is to convert data strings into Date format using ISODate function during the insertion. But, if you want to process the data you already inserted, you can do it with the following code using mongodb console:

db.collection.find().forEach(function(element){
  element.OrderDate = ISODate(element.OrderDate);
  db.collection.save(element);
})

此代码将处理集合 collection 中的每个元素,并将 Orderdate 字段的类型从 String 更改为 Date.

This code will process each element in your collection collection and change the type of Orderdate field from String to Date.

这篇关于如何从字符串转换为日期数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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