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

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

问题描述

>

将数据从csv文件上传到MongoDB。它将 OrderDate 作为字符串数据类型,由于使用BI工具创建报表时遇到问题。
我的集合中有大约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 to Date with a single query?

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 更改为日期

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

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

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