我正在将数据从 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.
更多推荐
如何从字符串转换为日期数据类型?
发布评论