# Bulk Update Records API

> Effortlessly update multiple records at once based on queries—streamline batch data operations.

Source: https://docs.apimaker.dev/v1/examples/sys/db/gen/updateManyGen.html

<!-- --8<-- [start:body] -->

`g.sys.db.gen.updateManyGen` calls the [update many generated API](/v1/docs/apis-all/generated-apis/auto-generated-update-many-api.html) from your code : no schema, the data as it is in the table.

## Structure
```typescript title="Update many" linenums="1"
let updateMany = await g.sys.db.gen.updateManyGen({
    instance: "INSTANCE_NAME", database: 'DB_NAME', collection: "COLLECTION_NAME",
    find: {
        "id": {
            "$in": [1018, 1019]
        }
    },
    updateData: {
        "city_name": "DELHI"
    }
});
```

## Simple update-many
```typescript title="Update many" linenums="1"
let updateMany = await g.sys.db.gen.updateManyGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "first_name": "Alice"
    },
    updateData: {
        "first_name": "MALLORY"
    }
});
```

## update-many with header
```typescript title="Update many with header" linenums="1"
let updateMany = await g.sys.db.gen.updateManyGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "first_name": "Alice"
    },
    updateData: {
        "first_name": "MALLORY"
    },
    headers: {
        "x-am-response-case": "capitalCase"
    }
});
```

## $in update-many
```typescript title="$in Update many" linenums="1"
let updateMany = await g.sys.db.gen.updateManyGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "customer_id": {
            "$in": [2, 3]
        }
    },
    updateData: {
        "first_name": "MALLORY"
    }
});
```

## $nin update-many
```typescript title="$nin Update many" linenums="1"
let updateMany = await g.sys.db.gen.updateManyGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "customer_id": {
            "$nin": [2, 3]
        }
    },
    updateData: {
        "first_name": "MALLORY"
    }
});
```

## $and update-many
```typescript title="$and Update many" linenums="1"
let updateMany = await g.sys.db.gen.updateManyGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        $and: [
            {
                "first_name": "Alice"
            },
            {
                "isActive": 1
            }
        ]
    },
    updateData: {
        "first_name": "MALLORY"
    }
});
```

## $or update-many
```typescript title="$or Update many" linenums="1"
let updateMany = await g.sys.db.gen.updateManyGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        $or: [
            {
                "first_name": "Alice"
            },
            {
                "isActive": 1
            }
        ]
    },
    updateData: {
        "first_name": "MALLORY"
    }
});
```

## $eq update-many
```typescript title="$eq Update many" linenums="1"
let updateMany = await g.sys.db.gen.updateManyGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "customer_id": {
            "$eq": 2
        }
    },
    updateData: {
        "first_name": "MALLORY"
    }
});
```

## $gt update-many
```typescript title="$gt Update many" linenums="1"
let updateMany = await g.sys.db.gen.updateManyGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "customer_id": {
            "$gt": 2
        }
    },
    updateData: {
        "first_name": "MALLORY"
    }
});
```

## $gte update-many
```typescript title="$gte Update many" linenums="1"
let updateMany = await g.sys.db.gen.updateManyGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "customer_id": {
            "$gte": 2
        }
    },
    updateData: {
        "first_name": "MALLORY"
    }
});
```

## $lt update-many
```typescript title="$lt Update many" linenums="1"
let updateMany = await g.sys.db.gen.updateManyGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "customer_id": {
            "$lt": 2
        }
    },
    updateData: {
        "first_name": "MALLORY"
    }
});
```

## $lte update-many
```typescript title="$lte Update many" linenums="1"
let updateMany = await g.sys.db.gen.updateManyGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "customer_id": {
            "$lte": 2
        }
    },
    updateData: {
        "first_name": "MALLORY"
    }
});
```

## [mongoDB] update-many
```typescript title="$mongoDB Update many" linenums="1"
let updateMany = await g.sys.db.gen.updateManyGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "customer_id": 2,
        "ages.birth_year": "1889"
    },
    updateData: {
        "ages.$.age": "35"
    }
});
```

## [MongoDB] $elemMatch, $gte, $lte update-many
```typescript title="$elemMatch, $gte, $lte Update many" linenums="1"
let updateMany = await g.sys.db.gen.updateManyGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "customer_id": 2,
        "ages": {
            "$elemMatch": {
                "age": {
                    "$gte": 30
                },
                "birth_year": {
                    "$lte": 1900
                }
            }
        }
    },
    updateData: {
        "first_name": "MALLORY"
    }
});
```

## [MongoDB] $toObjectId
```typescript title="$toObjectId" linenums="1"
let updateMany = await g.sys.db.gen.updateManyGen({
    instance: "mongodb", database: "am_work", collection: "update_to_objectid",
    find: {
    },
    updateData: [{
        $set: {
            company_id: { $toObjectId: "$company_id" }
        }
    }]
});
```

## Multi-tenant
- `instance: "crm::acme"` works on the database of the tenant `acme` of the [multi-tenant](/v1/docs/features/multi-tenant.html) instance `crm`.
- With `instance: "crm"`, the call works on the database of the tenant of the request: a custom API called with the [x-am-tenant-username](/v1/docs/apis-all/header/requestHeader.html#x-am-tenant-username) header passes it on to the APIs it calls. A request without a tenant works on the structure database.

```typescript title="Update many customers of the tenant acme" linenums="1"
let updateMany = await g.sys.db.gen.updateManyGen({
instance: "crm::acme", database: "crm", collection: "customers",
    find: {
        "first_name": "Alice"
    },
    updateData: {
        "first_name": "MALLORY"
    }
});
```

<!-- --8<-- [end:body] -->

## Related

- [Update many generated API](/v1/docs/apis-all/generated-apis/auto-generated-update-many-api.html) · [Schema API from code](/v1/examples/sys/db/updateMany.html) · [The global object g](/v1/docs/pre-defined-terms/global-object-g.html)
