如何在 Mongodb 中将字符串数据类型大量更改为 int?

How can I change the string data type to int massively in Mongodb?

我有一个类似这样的数据库:

{
        "_id" : ObjectId("61b69c02f92253fbe1017f23"),
        "nombre" : "Judd",
        "primer_apellido" : "Saterthwait",
        "segundo_apellido" : "Wabb",
        "identificacion" : "929499610",
        "genero" : "M",
        "edad" : "22",
        "seguridad_social" : "0",
        "telefono" : "4796610146",
        "banco" : "BANK OF AMERICA",
        "cuenta" : "36687045607282",
        "ciudad" : "Lihu",
        "desarrollador" : "senior",
        "trabaja_en" : "Thoughtsphere",
        "correo" : "jwabb3j@admin.ch",
        "pass" : "aea6d8bed55166ecde96d8ca1c687234",
        "fec_ultimo_acceso" : "2021-04-30",
        "tiene_hijos" : "1",
        "carnetizado" : "0",
        "activo" : "0",
        "reportes" : "1"
}
{
        "_id" : ObjectId("61b69c02f92253fbe1017f39"),
        "nombre" : "Alleyn",
        "primer_apellido" : "Corneljes",
        "segundo_apellido" : "Andrivel",
        "identificacion" : "615842724",
        "genero" : "M",
        "edad" : "46",
        "telefono" : "6722395057",
        "banco" : "CITYBANK",
        "cuenta" : "4508531326650291",
        "ciudad" : "Sambonggede",
        "desarrollador" : "senior",
        "trabaja_en" : "Skyndu",
        "correo" : "aandrivelat@wix.com",
        "pass" : "a16ebf90ff6d116d220db8ede95ae253",
        "fec_ultimo_acceso" : "2021-04-30",
        "tiene_hijos" : "1",
        "carnetizado" : "1",
        "activo" : "0",
        "reportes" : "2"
}
{
        "_id" : ObjectId("61b69c02f92253fbe1017f75"),
        "nombre" : "Tessa",
        "primer_apellido" : "Biesinger",
        "segundo_apellido" : "De Meis",
        "identificacion" : "12958583",
        "genero" : "F",
        "edad" : "50",
        "seguridad_social" : "1",
        "telefono" : "7465255759",
        "banco" : "GM2",
        "cuenta" : "6709265650990708",
        "ciudad" : "Wuyang",
        "desarrollador" : "junior",
        "trabaja_en" : "Babbleset",
        "correo" : "tdemeis7s@wired.com",
        "pass" : "0f4e5cee2132609be62102ab945c723a",
        "fec_ultimo_acceso" : "2021-04-30",
        "tiene_hijos" : "1",
        "carnetizado" : "0",
        "activo" : "1",
        "reportes" : "2"
}

发生的事情是,在 'edad' 键(英文年龄)中,它是一个数字,但在字符串中,例如:“45”。我想将数据库中存储的所有对象的数据类型从字符串“45”更改为数字 45。我该怎么做? (我认为是 forEach)

查询

  • 管道更新需要 MongoDB >= 4.2
  • 使用 $toInt 聚合运算符将年龄字段从字符串更改为整数,更改为所有文档({} 匹配所有文档)。
  • 使用驱动程序的 updateMany 方法或设置选项 {"multi" : true} 更新所有文档。

Test code here

update({},[{"$set": {"edad": {"$toInt": "$edad"}}}],{"multi" : true})