# Array Operations

> Manipulate array fields within documents using operations like push, pull, and pop in API Maker.

Source: https://docs.apimaker.dev/v4/examples/sys/db/arrayOperations.html

> **The code in every language**
>
> Each example is the inside of `main`, written in every [language](https://docs.apimaker.dev/v4/docs/languages/index.html) API Maker runs : pick yours in the tabs, and every page shows it from then on. The types a call takes (`GetAll`, `DeepItem`…) are those of `am` : `from am import GetAll` in Python, `use am::GetAll;` in Rust, `import am.GetAll;` in Java and Kotlin ; PHP and C# have them without an import, Go names them `am.GetAll`.

`g.sys.db.arrayOperations` calls the [array operations schema API](https://docs.apimaker.dev/v4/docs/apis-all/schema-apis/auto-generated-schema-based-array-operations-api.html) of any table from your code, with the same headers and query parameters as over HTTP. It returns the data. The whole envelope (`success`, `statusCode`, `data`, `totalCount`, `errors`…) comes back when `true` is passed as the second argument in TypeScript, and through `full` in the other languages : `g.sys.db.full` (`g.Sys.Db.Full` in C# and Go, `$g->sys->db->full` in PHP).

## Array operation [push]

**TypeScript**

**Array operation push**

```typescript
await g.sys.db.arrayOperations({
    instance: "mongodb", database: "inventory", collection: "customers",
    find: {
        "customer_id": 1
    },
    operations: [
        {
            operation: "push",
            path: "attributes.color",
            dataToPush: [
                "YELLOW",
                "SKY_BLUE"
            ]
        }
    ]
});
```

**Python**

**Array operation push**

```python
await g.sys.db.array_operations(
    'mongodb', 'inventory', 'customers',
    find={
        'customer_id': 1,
    },
    operations=[
        ArrayOperation(
            'push',
            'attributes.color',
            data_to_push=[
                'YELLOW',
                'SKY_BLUE',
            ],
        ),
    ],
)
```

**PHP**

**Array operation push**

```php
$g->sys->db->arrayOperations(
    'mongodb', 'inventory', 'customers',
    find: [
        'customer_id' => 1,
    ],
    operations: [
        new ArrayOperation(
            'push',
            'attributes.color',
            dataToPush: [
                'YELLOW',
                'SKY_BLUE',
            ],
        ),
    ],
);
```

**C#**

**Array operation push**

```csharp
await g.Sys.Db.ArrayOperations(new ArrayOperations("mongodb", "inventory", "customers")
{
    Find = new JsonObject
    {
        ["customer_id"] = 1,
    },
    Operations = [
        new ArrayOperation("push", "attributes.color")
        {
            DataToPush = [
                "YELLOW",
                "SKY_BLUE",
            ],
        },
    ],
});
```

**Go**

**Array operation push**

```go
if _, err := g.Sys.Db.ArrayOperations(am.ArrayOperations{
	Instance: "mongodb", Database: "inventory", Collection: "customers",
	Find: am.M{
		"customer_id": 1,
	},
	Operations: []am.ArrayOperation{
		{
			Operation: "push",
			Path:      "attributes.color",
			DataToPush: []any{
				"YELLOW",
				"SKY_BLUE",
			},
		},
	},
}); err != nil {
	return nil, err
}
```

**Rust**

**Array operation push**

```rust
g.sys.db.array_operations(
    ArrayOperations::new("mongodb", "inventory", "customers")
        .find(json!({
            "customer_id": 1,
        }))
        .operations([
            ArrayOperation::new("push", "attributes.color")
                .data_to_push([
                    "YELLOW",
                    "SKY_BLUE",
                ]),
        ]),
)?;
```

**Java**

**Array operation push**

```java
g.sys.db.arrayOperations(new ArrayOperations("mongodb", "inventory", "customers")
    .find(Map.of(
        "customer_id", 1
    ))
    .operations(
        new ArrayOperation("push", "attributes.color")
            .dataToPush(
                "YELLOW",
                "SKY_BLUE"
            )
    ));
```

**Kotlin**

**Array operation push**

```kotlin
g.sys.db.arrayOperations(
    ArrayOperations("mongodb", "inventory", "customers")
        .find(mapOf(
            "customer_id" to 1,
        ))
        .operations(
            ArrayOperation("push", "attributes.color")
                .dataToPush(
                    "YELLOW",
                    "SKY_BLUE",
                ),
        ),
)
```

## Array operation [pull]

**TypeScript**

**Array operation pull**

```typescript
await g.sys.db.arrayOperations({
    instance: "mongodb", database: "inventory", collection: "customers",
    find: {
        "customer_id": 1
    },
    operations: [
        {
            operation: "pull",
            path: "ages",
            queryToRemove: {
                "age": 2,
                "birth_year": 1989,
                "country_id": 1
            }
        }
    ]
});
```

**Python**

**Array operation pull**

```python
await g.sys.db.array_operations(
    'mongodb', 'inventory', 'customers',
    find={
        'customer_id': 1,
    },
    operations=[
        ArrayOperation(
            'pull',
            'ages',
            query_to_remove={
                'age': 2,
                'birth_year': 1989,
                'country_id': 1,
            },
        ),
    ],
)
```

**PHP**

**Array operation pull**

```php
$g->sys->db->arrayOperations(
    'mongodb', 'inventory', 'customers',
    find: [
        'customer_id' => 1,
    ],
    operations: [
        new ArrayOperation(
            'pull',
            'ages',
            queryToRemove: [
                'age' => 2,
                'birth_year' => 1989,
                'country_id' => 1,
            ],
        ),
    ],
);
```

**C#**

**Array operation pull**

```csharp
await g.Sys.Db.ArrayOperations(new ArrayOperations("mongodb", "inventory", "customers")
{
    Find = new JsonObject
    {
        ["customer_id"] = 1,
    },
    Operations = [
        new ArrayOperation("pull", "ages")
        {
            QueryToRemove = new JsonObject
            {
                ["age"] = 2,
                ["birth_year"] = 1989,
                ["country_id"] = 1,
            },
        },
    ],
});
```

**Go**

**Array operation pull**

```go
if _, err := g.Sys.Db.ArrayOperations(am.ArrayOperations{
	Instance: "mongodb", Database: "inventory", Collection: "customers",
	Find: am.M{
		"customer_id": 1,
	},
	Operations: []am.ArrayOperation{
		{
			Operation: "pull",
			Path:      "ages",
			QueryToRemove: am.M{
				"age":        2,
				"birth_year": 1989,
				"country_id": 1,
			},
		},
	},
}); err != nil {
	return nil, err
}
```

**Rust**

**Array operation pull**

```rust
g.sys.db.array_operations(
    ArrayOperations::new("mongodb", "inventory", "customers")
        .find(json!({
            "customer_id": 1,
        }))
        .operations([
            ArrayOperation::new("pull", "ages")
                .query_to_remove(json!({
                    "age": 2,
                    "birth_year": 1989,
                    "country_id": 1,
                })),
        ]),
)?;
```

**Java**

**Array operation pull**

```java
g.sys.db.arrayOperations(new ArrayOperations("mongodb", "inventory", "customers")
    .find(Map.of(
        "customer_id", 1
    ))
    .operations(
        new ArrayOperation("pull", "ages")
            .queryToRemove(Map.of(
                "age", 2,
                "birth_year", 1989,
                "country_id", 1
            ))
    ));
```

**Kotlin**

**Array operation pull**

```kotlin
g.sys.db.arrayOperations(
    ArrayOperations("mongodb", "inventory", "customers")
        .find(mapOf(
            "customer_id" to 1,
        ))
        .operations(
            ArrayOperation("pull", "ages")
                .queryToRemove(mapOf(
                    "age" to 2,
                    "birth_year" to 1989,
                    "country_id" to 1,
                )),
        ),
)
```

## Array operation [pullAll]

**TypeScript**

**Array operation pullAll**

```typescript
await g.sys.db.arrayOperations({
    instance: "mongodb", database: "inventory", collection: "customers",
    find: {
        "customer_id": 1
    },
    operations: [
        {
            operation: "pullAll",
            path: "attributes.size",
            dataToPull: [
                3.5
            ]
        }
    ]
});
```

**Python**

**Array operation pullAll**

```python
await g.sys.db.array_operations(
    'mongodb', 'inventory', 'customers',
    find={
        'customer_id': 1,
    },
    operations=[
        ArrayOperation(
            'pullAll',
            'attributes.size',
            data_to_pull=[
                3.5,
            ],
        ),
    ],
)
```

**PHP**

**Array operation pullAll**

```php
$g->sys->db->arrayOperations(
    'mongodb', 'inventory', 'customers',
    find: [
        'customer_id' => 1,
    ],
    operations: [
        new ArrayOperation(
            'pullAll',
            'attributes.size',
            dataToPull: [
                3.5,
            ],
        ),
    ],
);
```

**C#**

**Array operation pullAll**

```csharp
await g.Sys.Db.ArrayOperations(new ArrayOperations("mongodb", "inventory", "customers")
{
    Find = new JsonObject
    {
        ["customer_id"] = 1,
    },
    Operations = [
        new ArrayOperation("pullAll", "attributes.size")
        {
            DataToPull = [
                3.5,
            ],
        },
    ],
});
```

**Go**

**Array operation pullAll**

```go
if _, err := g.Sys.Db.ArrayOperations(am.ArrayOperations{
	Instance: "mongodb", Database: "inventory", Collection: "customers",
	Find: am.M{
		"customer_id": 1,
	},
	Operations: []am.ArrayOperation{
		{
			Operation: "pullAll",
			Path:      "attributes.size",
			DataToPull: []any{
				3.5,
			},
		},
	},
}); err != nil {
	return nil, err
}
```

**Rust**

**Array operation pullAll**

```rust
g.sys.db.array_operations(
    ArrayOperations::new("mongodb", "inventory", "customers")
        .find(json!({
            "customer_id": 1,
        }))
        .operations([
            ArrayOperation::new("pullAll", "attributes.size")
                .data_to_pull([
                    json!(3.5),
                ]),
        ]),
)?;
```

**Java**

**Array operation pullAll**

```java
g.sys.db.arrayOperations(new ArrayOperations("mongodb", "inventory", "customers")
    .find(Map.of(
        "customer_id", 1
    ))
    .operations(
        new ArrayOperation("pullAll", "attributes.size")
            .dataToPull(
                3.5
            )
    ));
```

**Kotlin**

**Array operation pullAll**

```kotlin
g.sys.db.arrayOperations(
    ArrayOperations("mongodb", "inventory", "customers")
        .find(mapOf(
            "customer_id" to 1,
        ))
        .operations(
            ArrayOperation("pullAll", "attributes.size")
                .dataToPull(
                    3.5,
                ),
        ),
)
```

## Array operation [pop]

**TypeScript**

**Array operation pop**

```typescript
await g.sys.db.arrayOperations({
    instance: "mongodb", database: "inventory", collection: "customers",
    find: {
        "customer_id": 1
    },
    operations: [
        {
            operation: "pop",
            path: "attributes.color",
            direction: 1
        }
    ]
});
```

**Python**

**Array operation pop**

```python
await g.sys.db.array_operations(
    'mongodb', 'inventory', 'customers',
    find={
        'customer_id': 1,
    },
    operations=[
        ArrayOperation(
            'pop',
            'attributes.color',
            direction=1,
        ),
    ],
)
```

**PHP**

**Array operation pop**

```php
$g->sys->db->arrayOperations(
    'mongodb', 'inventory', 'customers',
    find: [
        'customer_id' => 1,
    ],
    operations: [
        new ArrayOperation(
            'pop',
            'attributes.color',
            direction: 1,
        ),
    ],
);
```

**C#**

**Array operation pop**

```csharp
await g.Sys.Db.ArrayOperations(new ArrayOperations("mongodb", "inventory", "customers")
{
    Find = new JsonObject
    {
        ["customer_id"] = 1,
    },
    Operations = [
        new ArrayOperation("pop", "attributes.color")
        {
            Direction = 1,
        },
    ],
});
```

**Go**

**Array operation pop**

```go
if _, err := g.Sys.Db.ArrayOperations(am.ArrayOperations{
	Instance: "mongodb", Database: "inventory", Collection: "customers",
	Find: am.M{
		"customer_id": 1,
	},
	Operations: []am.ArrayOperation{
		{
			Operation: "pop",
			Path:      "attributes.color",
			Direction: 1,
		},
	},
}); err != nil {
	return nil, err
}
```

**Rust**

**Array operation pop**

```rust
g.sys.db.array_operations(
    ArrayOperations::new("mongodb", "inventory", "customers")
        .find(json!({
            "customer_id": 1,
        }))
        .operations([
            ArrayOperation::new("pop", "attributes.color")
                .direction(1),
        ]),
)?;
```

**Java**

**Array operation pop**

```java
g.sys.db.arrayOperations(new ArrayOperations("mongodb", "inventory", "customers")
    .find(Map.of(
        "customer_id", 1
    ))
    .operations(
        new ArrayOperation("pop", "attributes.color")
            .direction(1)
    ));
```

**Kotlin**

**Array operation pop**

```kotlin
g.sys.db.arrayOperations(
    ArrayOperations("mongodb", "inventory", "customers")
        .find(mapOf(
            "customer_id" to 1,
        ))
        .operations(
            ArrayOperation("pop", "attributes.color")
                .direction(1),
        ),
)
```

## Array operation [set]

**TypeScript**

**Array operation set**

```typescript
await g.sys.db.arrayOperations({
    instance: "mongodb", database: "inventory", collection: "customers",
    find: {
        "customer_id": 1
    },
    operations: [
        {
            operation: "set",
            dataToSet: {
                'ages.birth_year': 1890,
            },
            arrayFilters: [
                {
                    'item.age': 4,
                },
            ],
        }
    ]
});
```

**Python**

**Array operation set**

```python
await g.sys.db.array_operations(
    'mongodb', 'inventory', 'customers',
    find={
        'customer_id': 1,
    },
    operations=[
        ArrayOperation(
            'set',
            data_to_set={
                'ages.birth_year': 1890,
            },
            array_filters=[
                {
                    'item.age': 4,
                },
            ],
        ),
    ],
)
```

**PHP**

**Array operation set**

```php
$g->sys->db->arrayOperations(
    'mongodb', 'inventory', 'customers',
    find: [
        'customer_id' => 1,
    ],
    operations: [
        new ArrayOperation(
            'set',
            dataToSet: [
                'ages.birth_year' => 1890,
            ],
            arrayFilters: [
                [
                    'item.age' => 4,
                ],
            ],
        ),
    ],
);
```

**C#**

**Array operation set**

```csharp
await g.Sys.Db.ArrayOperations(new ArrayOperations("mongodb", "inventory", "customers")
{
    Find = new JsonObject
    {
        ["customer_id"] = 1,
    },
    Operations = [
        new ArrayOperation
        {
            Operation = "set",
            DataToSet = new JsonObject
            {
                ["ages.birth_year"] = 1890,
            },
            ArrayFilters = [
                new JsonObject
                {
                    ["item.age"] = 4,
                },
            ],
        },
    ],
});
```

**Go**

**Array operation set**

```go
if _, err := g.Sys.Db.ArrayOperations(am.ArrayOperations{
	Instance: "mongodb", Database: "inventory", Collection: "customers",
	Find: am.M{
		"customer_id": 1,
	},
	Operations: []am.ArrayOperation{
		{
			Operation: "set",
			DataToSet: am.M{
				"ages.birth_year": 1890,
			},
			ArrayFilters: []any{
				am.M{
					"item.age": 4,
				},
			},
		},
	},
}); err != nil {
	return nil, err
}
```

**Rust**

**Array operation set**

```rust
g.sys.db.array_operations(
    ArrayOperations::new("mongodb", "inventory", "customers")
        .find(json!({
            "customer_id": 1,
        }))
        .operations([
            ArrayOperation::default()
                .operation("set")
                .data_to_set(json!({
                    "ages.birth_year": 1890,
                }))
                .array_filters([
                    json!({
                        "item.age": 4,
                    }),
                ]),
        ]),
)?;
```

**Java**

**Array operation set**

```java
g.sys.db.arrayOperations(new ArrayOperations("mongodb", "inventory", "customers")
    .find(Map.of(
        "customer_id", 1
    ))
    .operations(
        new ArrayOperation()
            .operation("set")
            .dataToSet(Map.of(
                "ages.birth_year", 1890
            ))
            .arrayFilters(
                Map.of(
                    "item.age", 4
                )
            )
    ));
```

**Kotlin**

**Array operation set**

```kotlin
g.sys.db.arrayOperations(
    ArrayOperations("mongodb", "inventory", "customers")
        .find(mapOf(
            "customer_id" to 1,
        ))
        .operations(
            ArrayOperation()
                .operation("set")
                .dataToSet(mapOf(
                    "ages.birth_year" to 1890,
                ))
                .arrayFilters(
                    mapOf(
                        "item.age" to 4,
                    ),
                ),
        ),
)
```

## Array operation [addToSet]

**TypeScript**

**Array operation addToSet**

```typescript
await g.sys.db.arrayOperations({
    instance: "mongodb", database: "inventory", collection: "customers",
    find: {
        "customer_id": 1
    },
    operations: [
        {
            operation: "addToSet",
            dataToSet: {
                'ages.birth_year': 1890,
            },
            arrayFilters: [
                {
                    'item.age': 4,
                },
            ],
        }
    ]
});
```

**Python**

**Array operation addToSet**

```python
await g.sys.db.array_operations(
    'mongodb', 'inventory', 'customers',
    find={
        'customer_id': 1,
    },
    operations=[
        ArrayOperation(
            'addToSet',
            data_to_set={
                'ages.birth_year': 1890,
            },
            array_filters=[
                {
                    'item.age': 4,
                },
            ],
        ),
    ],
)
```

**PHP**

**Array operation addToSet**

```php
$g->sys->db->arrayOperations(
    'mongodb', 'inventory', 'customers',
    find: [
        'customer_id' => 1,
    ],
    operations: [
        new ArrayOperation(
            'addToSet',
            dataToSet: [
                'ages.birth_year' => 1890,
            ],
            arrayFilters: [
                [
                    'item.age' => 4,
                ],
            ],
        ),
    ],
);
```

**C#**

**Array operation addToSet**

```csharp
await g.Sys.Db.ArrayOperations(new ArrayOperations("mongodb", "inventory", "customers")
{
    Find = new JsonObject
    {
        ["customer_id"] = 1,
    },
    Operations = [
        new ArrayOperation
        {
            Operation = "addToSet",
            DataToSet = new JsonObject
            {
                ["ages.birth_year"] = 1890,
            },
            ArrayFilters = [
                new JsonObject
                {
                    ["item.age"] = 4,
                },
            ],
        },
    ],
});
```

**Go**

**Array operation addToSet**

```go
if _, err := g.Sys.Db.ArrayOperations(am.ArrayOperations{
	Instance: "mongodb", Database: "inventory", Collection: "customers",
	Find: am.M{
		"customer_id": 1,
	},
	Operations: []am.ArrayOperation{
		{
			Operation: "addToSet",
			DataToSet: am.M{
				"ages.birth_year": 1890,
			},
			ArrayFilters: []any{
				am.M{
					"item.age": 4,
				},
			},
		},
	},
}); err != nil {
	return nil, err
}
```

**Rust**

**Array operation addToSet**

```rust
g.sys.db.array_operations(
    ArrayOperations::new("mongodb", "inventory", "customers")
        .find(json!({
            "customer_id": 1,
        }))
        .operations([
            ArrayOperation::default()
                .operation("addToSet")
                .data_to_set(json!({
                    "ages.birth_year": 1890,
                }))
                .array_filters([
                    json!({
                        "item.age": 4,
                    }),
                ]),
        ]),
)?;
```

**Java**

**Array operation addToSet**

```java
g.sys.db.arrayOperations(new ArrayOperations("mongodb", "inventory", "customers")
    .find(Map.of(
        "customer_id", 1
    ))
    .operations(
        new ArrayOperation()
            .operation("addToSet")
            .dataToSet(Map.of(
                "ages.birth_year", 1890
            ))
            .arrayFilters(
                Map.of(
                    "item.age", 4
                )
            )
    ));
```

**Kotlin**

**Array operation addToSet**

```kotlin
g.sys.db.arrayOperations(
    ArrayOperations("mongodb", "inventory", "customers")
        .find(mapOf(
            "customer_id" to 1,
        ))
        .operations(
            ArrayOperation()
                .operation("addToSet")
                .dataToSet(mapOf(
                    "ages.birth_year" to 1890,
                ))
                .arrayFilters(
                    mapOf(
                        "item.age" to 4,
                    ),
                ),
        ),
)
```

## Structure

**TypeScript**

**Array operations**

```typescript
await g.sys.db.arrayOperations({
    instance: "INSTANCE_NAME", database: 'DB_NAME', collection: "COLLECTION_NAME",
    find: {
        "customer_id": 1
    },
    operations: [
        {
            operation: "push", // push | addToSet | pull | pullAll | pop | set
            path: "attributes.color",
            dataToPush: [
                "YELLOW",
                "SKY_BLUE"
            ]
        },
        {
            operation: "pull", // push | addToSet | pull | pullAll | pop | set
            path: "attributes.color",
            dataToPull: [
                "SKY_BLUE"
            ]
        }
    ],
    select: "COMMA_SEPARATED_COLUMN_NAMES",
    headers: {
        "x-am-response-case": "capitalCase", // noChange | camelCase | capitalCase | constantCase | dotCase | headerCase | noCase | paramCase | pascalCase | pathCase | sentenceCase | snakeCase
        "x-am-response-object-type": "make_flat", // no_action | make_flat
        "x-am-meta": "true", // true, false
        "x-am-internationalization": "USER_I18N_ID",
        "x-am-run-in-sandbox": "2",
        "x-am-content-type-response": "text/xml", // application/json | text/xml | text/yaml | text/plain | text/html | application/octet-stream
        "x-am-cache-control": "reset_cache", // no_action | reset_cache
        "x-am-get-encrypted-data": "get_only_encryption", // no_encryption | get_only_encryption | get_data_and_encryption
        "x-am-sandbox-timeout": "13000",
        "x-no-compression": "true", // true | false
        "x-am-encrypted-payload": "true", // user will send 'true', when payload is encrypted for transfer
        // Multi-tenant : the tenant of the request, each instance finds it in its own tenants table
        "x-am-tenant-username": "TENANT_USERNAME", // ex: acme
        // Authorization headers
        "x-am-authorization": "AUTHORIZATION_TOKEN",
        "x-am-user-authorization": "API_USER_TOKEN",
        "x-aws-authorization": "AWS_TOKEN",
        "x-google-authorization": "GOOGLE_TOKEN",
        "x-azure-authorization": "AZURE_TOKEN",
    },
});
```

**Python**

**Array operations**

```python
await g.sys.db.array_operations(
    'INSTANCE_NAME', 'DB_NAME', 'COLLECTION_NAME',
    find={
        'customer_id': 1,
    },
    operations=[
        ArrayOperation(
            'push',  # push | addToSet | pull | pullAll | pop | set
            'attributes.color',
            data_to_push=[
                'YELLOW',
                'SKY_BLUE',
            ],
        ),
        ArrayOperation(
            'pull',  # push | addToSet | pull | pullAll | pop | set
            'attributes.color',
            data_to_pull=[
                'SKY_BLUE',
            ],
        ),
    ],
    select='COMMA_SEPARATED_COLUMN_NAMES',
    headers={
        'x-am-response-case': 'capitalCase',  # noChange | camelCase | capitalCase | constantCase | dotCase | headerCase | noCase | paramCase | pascalCase | pathCase | sentenceCase | snakeCase
        'x-am-response-object-type': 'make_flat',  # no_action | make_flat
        'x-am-meta': 'true',  # true, false
        'x-am-internationalization': 'USER_I18N_ID',
        'x-am-run-in-sandbox': '2',
        'x-am-content-type-response': 'text/xml',  # application/json | text/xml | text/yaml | text/plain | text/html | application/octet-stream
        'x-am-cache-control': 'reset_cache',  # no_action | reset_cache
        'x-am-get-encrypted-data': 'get_only_encryption',  # no_encryption | get_only_encryption | get_data_and_encryption
        'x-am-sandbox-timeout': '13000',
        'x-no-compression': 'true',  # true | false
        'x-am-encrypted-payload': 'true',  # user will send 'true', when payload is encrypted for transfer
        # Multi-tenant : the tenant of the request, each instance finds it in its own tenants table
        'x-am-tenant-username': 'TENANT_USERNAME',  # ex: acme
        # Authorization headers
        'x-am-authorization': 'AUTHORIZATION_TOKEN',
        'x-am-user-authorization': 'API_USER_TOKEN',
        'x-aws-authorization': 'AWS_TOKEN',
        'x-google-authorization': 'GOOGLE_TOKEN',
        'x-azure-authorization': 'AZURE_TOKEN',
    },
)
```

**PHP**

**Array operations**

```php
$g->sys->db->arrayOperations(
    'INSTANCE_NAME', 'DB_NAME', 'COLLECTION_NAME',
    find: [
        'customer_id' => 1,
    ],
    operations: [
        new ArrayOperation(
            'push', // push | addToSet | pull | pullAll | pop | set
            'attributes.color',
            dataToPush: [
                'YELLOW',
                'SKY_BLUE',
            ],
        ),
        new ArrayOperation(
            'pull', // push | addToSet | pull | pullAll | pop | set
            'attributes.color',
            dataToPull: [
                'SKY_BLUE',
            ],
        ),
    ],
    select: 'COMMA_SEPARATED_COLUMN_NAMES',
    headers: [
        'x-am-response-case' => 'capitalCase', // noChange | camelCase | capitalCase | constantCase | dotCase | headerCase | noCase | paramCase | pascalCase | pathCase | sentenceCase | snakeCase
        'x-am-response-object-type' => 'make_flat', // no_action | make_flat
        'x-am-meta' => 'true', // true, false
        'x-am-internationalization' => 'USER_I18N_ID',
        'x-am-run-in-sandbox' => '2',
        'x-am-content-type-response' => 'text/xml', // application/json | text/xml | text/yaml | text/plain | text/html | application/octet-stream
        'x-am-cache-control' => 'reset_cache', // no_action | reset_cache
        'x-am-get-encrypted-data' => 'get_only_encryption', // no_encryption | get_only_encryption | get_data_and_encryption
        'x-am-sandbox-timeout' => '13000',
        'x-no-compression' => 'true', // true | false
        'x-am-encrypted-payload' => 'true', // user will send 'true', when payload is encrypted for transfer
        // Multi-tenant : the tenant of the request, each instance finds it in its own tenants table
        'x-am-tenant-username' => 'TENANT_USERNAME', // ex: acme
        // Authorization headers
        'x-am-authorization' => 'AUTHORIZATION_TOKEN',
        'x-am-user-authorization' => 'API_USER_TOKEN',
        'x-aws-authorization' => 'AWS_TOKEN',
        'x-google-authorization' => 'GOOGLE_TOKEN',
        'x-azure-authorization' => 'AZURE_TOKEN',
    ],
);
```

**C#**

**Array operations**

```csharp
await g.Sys.Db.ArrayOperations(new ArrayOperations("INSTANCE_NAME", "DB_NAME", "COLLECTION_NAME")
{
    Find = new JsonObject
    {
        ["customer_id"] = 1,
    },
    Operations = [
        new ArrayOperation("push", "attributes.color")
        {
            DataToPush = [
                "YELLOW",
                "SKY_BLUE",
            ],
        },
        new ArrayOperation("pull", "attributes.color")
        {
            DataToPull = [
                "SKY_BLUE",
            ],
        },
    ],
    Select = "COMMA_SEPARATED_COLUMN_NAMES",
    Headers = new JsonObject
    {
        ["x-am-response-case"] = "capitalCase", // noChange | camelCase | capitalCase | constantCase | dotCase | headerCase | noCase | paramCase | pascalCase | pathCase | sentenceCase | snakeCase
        ["x-am-response-object-type"] = "make_flat", // no_action | make_flat
        ["x-am-meta"] = "true", // true, false
        ["x-am-internationalization"] = "USER_I18N_ID",
        ["x-am-run-in-sandbox"] = "2",
        ["x-am-content-type-response"] = "text/xml", // application/json | text/xml | text/yaml | text/plain | text/html | application/octet-stream
        ["x-am-cache-control"] = "reset_cache", // no_action | reset_cache
        ["x-am-get-encrypted-data"] = "get_only_encryption", // no_encryption | get_only_encryption | get_data_and_encryption
        ["x-am-sandbox-timeout"] = "13000",
        ["x-no-compression"] = "true", // true | false
        ["x-am-encrypted-payload"] = "true", // user will send 'true', when payload is encrypted for transfer
        // Multi-tenant : the tenant of the request, each instance finds it in its own tenants table
        ["x-am-tenant-username"] = "TENANT_USERNAME", // ex: acme
        // Authorization headers
        ["x-am-authorization"] = "AUTHORIZATION_TOKEN",
        ["x-am-user-authorization"] = "API_USER_TOKEN",
        ["x-aws-authorization"] = "AWS_TOKEN",
        ["x-google-authorization"] = "GOOGLE_TOKEN",
        ["x-azure-authorization"] = "AZURE_TOKEN",
    },
});
```

**Go**

**Array operations**

```go
if _, err := g.Sys.Db.ArrayOperations(am.ArrayOperations{
	Instance: "INSTANCE_NAME", Database: "DB_NAME", Collection: "COLLECTION_NAME",
	Find: am.M{
		"customer_id": 1,
	},
	Operations: []am.ArrayOperation{
		{
			Operation: "push", // push | addToSet | pull | pullAll | pop | set
			Path:      "attributes.color",
			DataToPush: []any{
				"YELLOW",
				"SKY_BLUE",
			},
		},
		{
			Operation: "pull", // push | addToSet | pull | pullAll | pop | set
			Path:      "attributes.color",
			DataToPull: []any{
				"SKY_BLUE",
			},
		},
	},
	Select: "COMMA_SEPARATED_COLUMN_NAMES",
	Headers: am.M{
		"x-am-response-case":         "capitalCase", // noChange | camelCase | capitalCase | constantCase | dotCase | headerCase | noCase | paramCase | pascalCase | pathCase | sentenceCase | snakeCase
		"x-am-response-object-type":  "make_flat",   // no_action | make_flat
		"x-am-meta":                  "true",        // true, false
		"x-am-internationalization":  "USER_I18N_ID",
		"x-am-run-in-sandbox":        "2",
		"x-am-content-type-response": "text/xml",            // application/json | text/xml | text/yaml | text/plain | text/html | application/octet-stream
		"x-am-cache-control":         "reset_cache",         // no_action | reset_cache
		"x-am-get-encrypted-data":    "get_only_encryption", // no_encryption | get_only_encryption | get_data_and_encryption
		"x-am-sandbox-timeout":       "13000",
		"x-no-compression":           "true", // true | false
		"x-am-encrypted-payload":     "true", // user will send 'true', when payload is encrypted for transfer
		// Multi-tenant : the tenant of the request, each instance finds it in its own tenants table
		"x-am-tenant-username": "TENANT_USERNAME", // ex: acme
		// Authorization headers
		"x-am-authorization":      "AUTHORIZATION_TOKEN",
		"x-am-user-authorization": "API_USER_TOKEN",
		"x-aws-authorization":     "AWS_TOKEN",
		"x-google-authorization":  "GOOGLE_TOKEN",
		"x-azure-authorization":   "AZURE_TOKEN",
	},
}); err != nil {
	return nil, err
}
```

**Rust**

**Array operations**

```rust
g.sys.db.array_operations(
    ArrayOperations::new("INSTANCE_NAME", "DB_NAME", "COLLECTION_NAME")
        .find(json!({
            "customer_id": 1,
        }))
        .operations([
            ArrayOperation::new("push", "attributes.color")
                .data_to_push([
                    "YELLOW",
                    "SKY_BLUE",
                ]),
            ArrayOperation::new("pull", "attributes.color")
                .data_to_pull([
                    "SKY_BLUE",
                ]),
        ])
        .select("COMMA_SEPARATED_COLUMN_NAMES")
        .headers(json!({
            "x-am-response-case": "capitalCase", // noChange | camelCase | capitalCase | constantCase | dotCase | headerCase | noCase | paramCase | pascalCase | pathCase | sentenceCase | snakeCase
            "x-am-response-object-type": "make_flat", // no_action | make_flat
            "x-am-meta": "true", // true, false
            "x-am-internationalization": "USER_I18N_ID",
            "x-am-run-in-sandbox": "2",
            "x-am-content-type-response": "text/xml", // application/json | text/xml | text/yaml | text/plain | text/html | application/octet-stream
            "x-am-cache-control": "reset_cache", // no_action | reset_cache
            "x-am-get-encrypted-data": "get_only_encryption", // no_encryption | get_only_encryption | get_data_and_encryption
            "x-am-sandbox-timeout": "13000",
            "x-no-compression": "true", // true | false
            "x-am-encrypted-payload": "true", // user will send 'true', when payload is encrypted for transfer
            // Multi-tenant : the tenant of the request, each instance finds it in its own tenants table
            "x-am-tenant-username": "TENANT_USERNAME", // ex: acme
            // Authorization headers
            "x-am-authorization": "AUTHORIZATION_TOKEN",
            "x-am-user-authorization": "API_USER_TOKEN",
            "x-aws-authorization": "AWS_TOKEN",
            "x-google-authorization": "GOOGLE_TOKEN",
            "x-azure-authorization": "AZURE_TOKEN",
        })),
)?;
```

**Java**

**Array operations**

```java
g.sys.db.arrayOperations(new ArrayOperations("INSTANCE_NAME", "DB_NAME", "COLLECTION_NAME")
    .find(Map.of(
        "customer_id", 1
    ))
    .operations(
        new ArrayOperation("push", "attributes.color")
            .dataToPush(
                "YELLOW",
                "SKY_BLUE"
            ),
        new ArrayOperation("pull", "attributes.color")
            .dataToPull(
                "SKY_BLUE"
            )
    )
    .select("COMMA_SEPARATED_COLUMN_NAMES")
    .headers(JsonObject.of(
        "x-am-response-case", "capitalCase", // noChange | camelCase | capitalCase | constantCase | dotCase | headerCase | noCase | paramCase | pascalCase | pathCase | sentenceCase | snakeCase
        "x-am-response-object-type", "make_flat", // no_action | make_flat
        "x-am-meta", "true", // true, false
        "x-am-internationalization", "USER_I18N_ID",
        "x-am-run-in-sandbox", "2",
        "x-am-content-type-response", "text/xml", // application/json | text/xml | text/yaml | text/plain | text/html | application/octet-stream
        "x-am-cache-control", "reset_cache", // no_action | reset_cache
        "x-am-get-encrypted-data", "get_only_encryption", // no_encryption | get_only_encryption | get_data_and_encryption
        "x-am-sandbox-timeout", "13000",
        "x-no-compression", "true", // true | false
        "x-am-encrypted-payload", "true", // user will send 'true', when payload is encrypted for transfer
        // Multi-tenant : the tenant of the request, each instance finds it in its own tenants table
        "x-am-tenant-username", "TENANT_USERNAME", // ex: acme
        // Authorization headers
        "x-am-authorization", "AUTHORIZATION_TOKEN",
        "x-am-user-authorization", "API_USER_TOKEN",
        "x-aws-authorization", "AWS_TOKEN",
        "x-google-authorization", "GOOGLE_TOKEN",
        "x-azure-authorization", "AZURE_TOKEN"
    )));
```

**Kotlin**

**Array operations**

```kotlin
g.sys.db.arrayOperations(
    ArrayOperations("INSTANCE_NAME", "DB_NAME", "COLLECTION_NAME")
        .find(mapOf(
            "customer_id" to 1,
        ))
        .operations(
            ArrayOperation("push", "attributes.color")
                .dataToPush(
                    "YELLOW",
                    "SKY_BLUE",
                ),
            ArrayOperation("pull", "attributes.color")
                .dataToPull(
                    "SKY_BLUE",
                ),
        )
        .select("COMMA_SEPARATED_COLUMN_NAMES")
        .headers(mapOf(
            "x-am-response-case" to "capitalCase", // noChange | camelCase | capitalCase | constantCase | dotCase | headerCase | noCase | paramCase | pascalCase | pathCase | sentenceCase | snakeCase
            "x-am-response-object-type" to "make_flat", // no_action | make_flat
            "x-am-meta" to "true", // true, false
            "x-am-internationalization" to "USER_I18N_ID",
            "x-am-run-in-sandbox" to "2",
            "x-am-content-type-response" to "text/xml", // application/json | text/xml | text/yaml | text/plain | text/html | application/octet-stream
            "x-am-cache-control" to "reset_cache", // no_action | reset_cache
            "x-am-get-encrypted-data" to "get_only_encryption", // no_encryption | get_only_encryption | get_data_and_encryption
            "x-am-sandbox-timeout" to "13000",
            "x-no-compression" to "true", // true | false
            "x-am-encrypted-payload" to "true", // user will send 'true', when payload is encrypted for transfer
            // Multi-tenant : the tenant of the request, each instance finds it in its own tenants table
            "x-am-tenant-username" to "TENANT_USERNAME", // ex: acme
            // Authorization headers
            "x-am-authorization" to "AUTHORIZATION_TOKEN",
            "x-am-user-authorization" to "API_USER_TOKEN",
            "x-aws-authorization" to "AWS_TOKEN",
            "x-google-authorization" to "GOOGLE_TOKEN",
            "x-azure-authorization" to "AZURE_TOKEN",
        )),
)
```

## Multi-tenant
- `instance: "crm::acme"` works on the database of the tenant `acme` of the [multi-tenant](https://docs.apimaker.dev/v4/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](https://docs.apimaker.dev/v4/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**

**Array operations on a customer of the tenant acme**

```typescript
let arrayOperations = await g.sys.db.arrayOperations({
    instance: "crm::acme", database: "crm", collection: "customers",
    find: {
        "customer_id": 1
    },
    operations: [
        {
            operation: "push",
            path: "tags",
            dataToPush: ["vip"]
        }
    ]
});
```

**Python**

**Array operations on a customer of the tenant acme**

```python
array_operations = await g.sys.db.array_operations(
    'crm::acme', 'crm', 'customers',
    find={
        'customer_id': 1,
    },
    operations=[
        ArrayOperation(
            'push',
            'tags',
            data_to_push=['vip'],
        ),
    ],
)
```

**PHP**

**Array operations on a customer of the tenant acme**

```php
$arrayOperations = $g->sys->db->arrayOperations(
    'crm::acme', 'crm', 'customers',
    find: [
        'customer_id' => 1,
    ],
    operations: [
        new ArrayOperation(
            'push',
            'tags',
            dataToPush: ['vip'],
        ),
    ],
);
```

**C#**

**Array operations on a customer of the tenant acme**

```csharp
var arrayOperations = await g.Sys.Db.ArrayOperations(new ArrayOperations("crm::acme", "crm", "customers")
{
    Find = new JsonObject
    {
        ["customer_id"] = 1,
    },
    Operations = [
        new ArrayOperation("push", "tags")
        {
            DataToPush = ["vip"],
        },
    ],
});
```

**Go**

**Array operations on a customer of the tenant acme**

```go
arrayOperations, err := g.Sys.Db.ArrayOperations(am.ArrayOperations{
	Instance: "crm::acme", Database: "crm", Collection: "customers",
	Find: am.M{
		"customer_id": 1,
	},
	Operations: []am.ArrayOperation{
		{
			Operation:  "push",
			Path:       "tags",
			DataToPush: []any{"vip"},
		},
	},
})
if err != nil {
	return nil, err
}
```

**Rust**

**Array operations on a customer of the tenant acme**

```rust
let array_operations = g.sys.db.array_operations(
    ArrayOperations::new("crm::acme", "crm", "customers")
        .find(json!({
            "customer_id": 1,
        }))
        .operations([
            ArrayOperation::new("push", "tags")
                .data_to_push(["vip"]),
        ]),
)?;
```

**Java**

**Array operations on a customer of the tenant acme**

```java
var arrayOperations = g.sys.db.arrayOperations(new ArrayOperations("crm::acme", "crm", "customers")
    .find(Map.of(
        "customer_id", 1
    ))
    .operations(
        new ArrayOperation("push", "tags")
            .dataToPush("vip")
    ));
```

**Kotlin**

**Array operations on a customer of the tenant acme**

```kotlin
val arrayOperations = g.sys.db.arrayOperations(
    ArrayOperations("crm::acme", "crm", "customers")
        .find(mapOf(
            "customer_id" to 1,
        ))
        .operations(
            ArrayOperation("push", "tags")
                .dataToPush("vip"),
        ),
)
```

## Without a schema

`g.sys.db.gen.arrayOperationsGen` takes the same arguments and calls the [array operations generated API](https://docs.apimaker.dev/v4/docs/apis-all/schema-apis/auto-generated-schema-based-array-operations-api.html#without-a-schema) : no schema, so the data is read and written as it is in the table, without the conversions, validations and defaults of the schema.

| Language | The call |
|---|---|
| TypeScript | `g.sys.db.gen.arrayOperationsGen(…)` |
| Python | `g.sys.db.gen.array_operations_gen(…)` |
| PHP | `$g->sys->db->gen->arrayOperationsGen(…)` |
| C#, Go | `g.Sys.Db.Gen.ArrayOperationsGen(…)` |
| Rust | `g.sys.db.array_operations_gen(ArrayOperations::new(…))` |
| Java, Kotlin | `g.sys.db.gen.arrayOperationsGen(…)` |

## Related

- [Array operations API](https://docs.apimaker.dev/v4/docs/apis-all/schema-apis/auto-generated-schema-based-array-operations-api.html) · [Generated API from code](https://docs.apimaker.dev/v4/examples/sys/db/arrayOperations.html#without-a-schema) · [The global object g](https://docs.apimaker.dev/v4/docs/pre-defined-terms/global-object-g.html)
