MongoDB之更改字段类型

在MongoDB的Collection中存在一些字段_updated_at原本应为Date类型,但是却为String类型的数据,因此需要更改String类型为Date类型。其中数据结构如下:
这里写图片描述
解决方法:
在MongoDB的shell中执行如下语句即可:

db.getCollection('Place').find({_updated_at: {$type; 2}}).forEach(function(doc) {
    db.getCollection('Place').updateOne({_id: doc._id},{$set: {_updated_at: new Date(doc.date)}})})

字段类型type的对应表如下:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/haiyanggeng/article/details/80250117