MongoDB 为数据库旧数据添加新字段方法

因mongodb在model里新建的新字段,不会映射到旧数据,故而需要批量为旧数据添加新字段

方法:

db.getCollection('数据表名称').updateMany({'新字段名称':{'$exists':false}},{'$set':{'新字段名称':''}})

以上可以理解为,筛选指定数据表中没有新字段的数据进行批量新增字段,并默认为'',当然''也可以为默认数字或其他类型。比如0

例:

db.getCollection('testcollection').updateMany({'test':{'$exists':false}},{'$set':{'test':''}})

Computer science and software engineering have always been my passion

posted @ 2021-01-19 18:36  游弋在冷风中  阅读(824)  评论(0)    收藏  举报