# Generate APIs

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

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

## Structure
```typescript
let getAll = await g.sys.db.gen.getAllGen({
    instance: "INSTANCE_NAME", database: 'DB_NAME', collection: "COLLECTION_NAME",
    queryParams: {
        deep: JSON.stringify([{
            s_key: "SOURCE_COLLECTION_COLUMN_NAME",
            t_key: "TARGET_COLLECTION_COLUMN_NAME",
            t_col: "TARGET_COLLECTION_NAME",
            select: "COLUMN_NAMES"
        }]),
        select: "COLUMN_NAMES",
        limit: 2,
        sort: "COLUMN_NAME"
    }
});
```

## Simple Get all with one condition

**Simple Get all from table**

```typescript
let getAll = await g.sys.db.gen.getAllGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    queryParams: {
        find: {
            first_name: 'James'
        }
    }
});
```

## With header

**Get all with header**

```typescript
let getAll = await g.sys.db.gen.getAllGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    headers: {
        "x-am-response-case": "capitalCase"
    }
});
```

## With header and queryParams

**Get all with header and queryParams**

```typescript
let getAll = await g.sys.db.gen.getAllGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    headers: {
        "x-am-meta": "true"
    },
    queryParams: {
        select: "first_name,last_name"
    }
});
```

## With multiple queryParams

**Get all with multiple queryParams**

```typescript
let getAll = await g.sys.db.gen.getAllGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    headers: {
        "x-am-response-case": "capitalCase"
    },
    queryParams: {
        select: "first_name,last_name",
        limit: 2
    }
});
```

## With all params

**Get all with all params**

```typescript
let getAll = await g.sys.db.gen.getAllGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    headers: {
        "x-am-response-case": "capitalCase"
    },
    queryParams: {
        select: "first_name,last_name",
        limit: 1,
        sort: "customer_id",
        deep: [{
            s_key: "customer_id",
            t_key: "owner_id",
            t_col: "products",
            select: "proName,proDescription,proPrice"
        }]
    }
});
```

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

**Get all customers of the tenant acme**

```typescript
let getAll = await g.sys.db.gen.getAllGen({
    instance: "crm::acme", database: "crm", collection: "customers",
    queryParams: {
        sort: "customer_id"
    }
});
```

**Get all customers of the tenant globex, named with the header of the call**

```typescript
let getAll = await g.sys.db.gen.getAllGen({
    instance: "crm", database: "crm", collection: "customers",
    headers: {
        "x-am-tenant-username": "globex"
    }
});
```

`g.sys.db.gen.getAllByStreamGen` calls the [get all by stream generated API](https://docs.apimaker.dev/v1/docs/apis-all/generated-apis/auto-generated-get-all-by-stream-api.html) from your code : no schema, the data as it is in the table.

## Structure

**Get all by stream**

```typescript
let response = [];
await g.sys.db.gen.getAllByStreamGen({
    instance: "INSTANCE_NAME", database: 'DB_NAME', collection: "COLLECTION_NAME",
    queryParams: {
        deep: `[{
            s_key: "SOURCE_COLLECTION_COLUMN_NAME",
            t_key: "TARGET_COLLECTION_COLUMN_NAME",
            t_col: "TARGET_COLLECTION_NAME",
            select: "COLUMN_NAMES"  
        }]`,
        select: "COLUMN_NAMES",
        sort: "-COLUMN_NAME",
        limit: 2,
        find: `{"price": 42187}`,
        skip: 1,
    }
}, (item) => {
    response.push(item);
});
```

## Simple Get all

**Simple Get all from table**

```typescript
let response = [];
await g.sys.db.gen.getAllByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
}, (item) => {
    response.push(item);
});
```

## With header

**Get all with header**

```typescript
let response = [];
await g.sys.db.gen.getAllByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    headers: {
        "x-am-response-case": "capitalCase"
    }
}, (item) => {
    response.push(item);
});
```

## With header and queryParams

**Get all with header and queryParams**

```typescript
let response = [];
await g.sys.db.gen.getAllByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    headers: {
        "x-am-response-case": "capitalCase"
    },
    queryParams: {
        select: "first_name,last_name"
    }
}, (item) => {
    response.push(item);
});
```

## With multiple queryParams

**Get all With multiple queryParams**

```typescript
let response = [];
await g.sys.db.gen.getAllByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    headers: {
        "x-am-response-case": "capitalCase"
    },
    queryParams: {
        select: "first_name,last_name",
        limit: 2
    }
}, (item) => {
    response.push(item);
});
```

## With all params

**Get all With all params**

```typescript
let response = [];
await g.sys.db.gen.getAllByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    headers: {
        "x-am-response-case": "capitalCase"
    },
    queryParams: {
        select: "first_name,last_name",
        limit: 1,
        sort: "customer_id",
    }
}, (item) => {
    response.push(item);
});
```

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

**Get all customers of the tenant acme by stream**

```typescript
let response = [];
await g.sys.db.gen.getAllByStreamGen({
    instance: "crm::acme", database: "crm", collection: "customers",
}, (item) => {
    response.push(item);
});
```

`g.sys.db.gen.getByIdGen` calls the [get by id generated API](https://docs.apimaker.dev/v1/docs/apis-all/generated-apis/auto-generated-get-by-id-api.html) from your code : no schema, the data as it is in the table.

## Structure

**Get by id**

```typescript
let getById = await g.sys.db.gen.getByIdGen({
    instance: "INSTANCE_NAME", database: 'DB_NAME', collection: "COLLECTION_NAME",
    id: 100050,
    primaryKey: 'COLUMN_NAME',
    select: "COLUMN_NAMES"
});
```

## Simple Get by id

**Simple Get by id from table**

```typescript
let getById = await g.sys.db.gen.getByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: 2
});
```

## With header

**Get by id with header**

```typescript
let getById = await g.sys.db.gen.getByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: 2,
    headers: {
        "x-am-response-case": "capitalCase"
    }
});
```

## With header and select

**Get by id with header and select**

```typescript
let getById = await g.sys.db.gen.getByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: 2,
    headers: {
        "x-am-response-case": "capitalCase"
    },
    select:"first_name,last_name"
});
```

## With primary key override

**Get by id with primary key override**

```typescript
let getById= await g.sys.db.gen.getByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: "NICK",
    primaryKey:"first_name",
});
```

## With all params

**Get by id with all params**

```typescript
let getAll = await g.sys.db.gen.getByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: "NICK",
    headers: {
        "x-am-response-case": "capitalCase"
    },
    select: "first_name,last_name",
    deep: [{ // string, string[], array of objects are supported
        s_key: "customer_id",
        t_key: "owner_id",
        t_col: "products",
        select: "proName,proDescription,proPrice"
    }],
    primaryKey: "first_name"
});
```

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

**Get the customer 1 of the tenant acme**

```typescript
let getById = await g.sys.db.gen.getByIdGen({
    instance: "crm::acme", database: "crm", collection: "customers",
    id: 1
});
```

`g.sys.db.gen.saveSingleOrMultipleGen` calls the [save single or multiple generated API](https://docs.apimaker.dev/v1/docs/apis-all/generated-apis/auto-generated-save-single-or-multiple-api.html) from your code : no schema, the data as it is in the table.

## Structure

**Save single/multiple structure**

```typescript
let saveSingleOrMultiple = await g.sys.db.gen.saveSingleOrMultipleGen({
    instance: "INSTANCE_NAME", database: 'DB_NAME', collection: "COLLECTION_NAME",
    saveData: {
        "field1": "fieldValue1",
        "field2": "fieldValue2"
    }
});
```

## Save single or multiple

**Save single/multiple**

```typescript
let saveSingleOrMultiple = await g.sys.db.gen.saveSingleOrMultipleGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    saveData: {
        "first_name": "Bob",
        "last_name": "Lin"
    }
});
```

## Save single or multiple with multiple data

**Save single/multiple**

```typescript
let saveSingleOrMultiple = await g.sys.db.gen.saveSingleOrMultipleGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    saveData: [{
        "first_name": "Bob",
        "last_name": "Lin"
    },
    {
        "first_name": "Alice",
        "last_name": "Page"
    }]
});
```

## with header

**Save single/multiple with header**

```typescript
let saveSingleOrMultiple = await g.sys.db.gen.saveSingleOrMultipleGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    saveData: {
        "first_name": "Bob",
        "last_name": "Lin"
    },
    headers: {
        "x-am-response-case": "capitalCase"
    }
});
```

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

**Save a customer of the tenant acme**

```typescript
let saveSingleOrMultiple = await g.sys.db.gen.saveSingleOrMultipleGen({
    instance: "crm::acme", database: "crm", collection: "customers",
    saveData: {
        "first_name": "Bob",
        "last_name": "Lin"
    }
});
```

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

## Structure

**Master save**

```typescript
let masterSave = await g.sys.db.gen.masterSaveGen({
    instance: "INSTANCE_NAME", database: 'DB_NAME', collection: "COLLECTION_NAME",
    saveData: {
        first_name: "James",
        last_name: "Bond"
    }
});
```

## master save single data

**Master save**

```typescript
let masterSave = await g.sys.db.gen.masterSaveGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    saveData: {
        "first_name": "Bob",
        "last_name": "Lin"
    }
});
```

## master save multiple data

**Master save**

```typescript
let masterSave = await g.sys.db.gen.masterSaveGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    saveData: [
        {
            "first_name": "Bob",
            "last_name": "Lin"
        },
        {
            "first_name": "Bob",
            "last_name": "Lin"
        }
    ]
});
```

## with header

**Master save with header**

```typescript
let masterSave = await g.sys.db.gen.masterSaveGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    saveData: {
        "first_name": "Bob",
        "last_name": "Lin"
    },
    headers: {
        "x-am-response-case": "capitalCase"
    }
});
```

## multiple database entry

**Master save with multy data[with help of schema configuration]**

```typescript
let masterSave = await g.sys.db.gen.masterSaveGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    saveData: {
        "city_name": "Wembley", //city_name save in mysql database
        "state_id": {
            "state_name": "London", //state_name save in maria database
            "country_id": {
                "country_name":"UK" //country_name save in postGreSql database
            }
        }
    },
    headers: {
        "x-am-response-case": "capitalCase"
    }
});
```

## multiple database multiple entry entry

**Master save with multy data[with help of schema configuration]**

```typescript
let masterSave = await g.sys.db.gen.masterSaveGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    saveData: [{
        "city_name": "Wembley",
        "state_id": {
            "state_name": "London",
            "country_id": 302
        }
    },
    {
        "city_name": "Buffalo",
        "state_id": {
            "state_name": "Chicago",
            "country_id": 303
        }
    }],
    headers: {
        "x-am-response-case": "capitalCase"
    }
});
```

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

**Master save a customer of the tenant acme**

```typescript
let masterSave = await g.sys.db.gen.masterSaveGen({
    instance: "crm::acme", database: "crm", collection: "customers",
    saveData: {
        "first_name": "Bob",
        "last_name": "Lin"
    }
});
```

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

### Schema-less Array Operations API

**Array operations**

```typescript
await g.sys.db.gen.arrayOperationsGen({
    instance: 'INSTANCE_NAME',
    database: 'DATABASE_NAME',
    collection: 'COLLECTION_NAME',
    find: {
        "id": 1
    },
    operations: [
        {
            operation: "push",
            path: "attributes.color",
            dataToPush: [
                "YELLOW",
                "SKY_BLUE"
            ]
        }
    ]
});
```

**Array operations headers support**

```typescript
await g.sys.db.gen.arrayOperationsGen({
    // ...
    headers: {
        "x-am-authorization": "TOKEN"
    }
});
```

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

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

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

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

## Structure

**Update by id**

```typescript
let updateById = await g.sys.db.gen.updateByIdGen({
    instance: "INSTANCE_NAME", database: 'DB_NAME', collection: "COLLECTION_NAME",
    id: 1008,
    primaryKey: "id",
    updateData: {
        "first_name": "Tanmay",
        "last_name": "Samay"
    },
    returnOriginal: true,
    select: "COLUMN_NAMES",
    upsert: false
});
```

## Simple updateById

**Update By Id**

```typescript
let updateById = await g.sys.db.gen.updateByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: 1,
    updateData: {
        "first_name": "MALLORY"
    }
});
```

## updateById primaryKey override

**Update By Id primarykey override**

```typescript
let updateById = await g.sys.db.gen.updateByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: "Alice",
    primaryKey: "first_name",
    updateData: {
        "first_name": "MALLORY"
    }
});
```

## updateById [select]

**Update By Id with select**

```typescript
let updateById = await g.sys.db.gen.updateByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: "Alice",
    primaryKey: "first_name",
    updateData: {
        "first_name": "MALLORY"
    },
    select: "first_name,last_name"
});
```

## updateById [headers]

**Update By Id with select**

```typescript
let updateById = await g.sys.db.gen.updateByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: "Alice",
    primaryKey: "first_name",
    updateData: {
        "first_name": "MALLORY"
    },
    select: "first_name,last_name",
    headers: {
        "x-am-response-case": "capitalCase"
    }
});
```

## updateById [upsert]

**Update By Id with upsert**

```typescript
let updateById = await g.sys.db.gen.updateByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: "Alice",
    primaryKey: "first_name",
    updateData: {
        "first_name": "MALLORY"
    },
    select: "first_name,last_name",
    headers: {
        "x-am-response-case": "capitalCase"
    },
    upsert: true
});
```

## updateById [returnDocument]

**Update By Id with upsert**

```typescript
let updateById = await g.sys.db.gen.updateByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: "Alice",
    primaryKey: "first_name",
    updateData: {
        "first_name": "MALLORY"
    },
    select: "first_name,last_name",
    headers: {
        "x-am-response-case": "capitalCase"
    },
    returnDocument: true 
});
```

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

**Update the customer 1 of the tenant acme**

```typescript
let updateById = await g.sys.db.gen.updateByIdGen({
    instance: "crm::acme", database: "crm", collection: "customers",
    id: 1,
    updateData: {
        "first_name": "MALLORY"
    }
});
```

`g.sys.db.gen.updateManyGen` calls the [update many generated API](https://docs.apimaker.dev/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

**Update many**

```typescript
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

**Update many**

```typescript
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

**Update many with header**

```typescript
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

**$in Update many**

```typescript
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

**$nin Update many**

```typescript
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

**$and Update many**

```typescript
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

**$or Update many**

```typescript
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

**$eq Update many**

```typescript
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

**$gt Update many**

```typescript
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

**$gte Update many**

```typescript
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

**$lt Update many**

```typescript
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

**$lte Update many**

```typescript
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

**$mongoDB Update many**

```typescript
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

**$elemMatch, $gte, $lte Update many**

```typescript
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

**$toObjectId**

```typescript
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](https://docs.apimaker.dev/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](https://docs.apimaker.dev/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.

**Update many customers of the tenant acme**

```typescript
let updateMany = await g.sys.db.gen.updateManyGen({
    instance: "crm::acme", database: "crm", collection: "customers",
    find: {
        "first_name": "Alice"
    },
    updateData: {
        "first_name": "MALLORY"
    }
});
```

`g.sys.db.gen.replaceByIdGen` calls the [replace by id generated API](https://docs.apimaker.dev/v1/docs/apis-all/generated-apis/auto-generated-replace-by-id-api.html) from your code : no schema, the data as it is in the table.

## Structure

**Replace by id**

```typescript
let replaceById = await g.sys.db.gen.replaceByIdGen({
    instance: "INSTANCE_NAME", database: 'DB_NAME', collection: "COLLECTION_NAME",
    id: 45855,
    primaryKey: "pincode",
    updateData: {
        "first_name": "Elonn",
        "last_name": "Mask"
    },
    returnOriginal: true,
    select: "COLUMN_NAMES",
    upsert: false
});
```

## Simple replaceById

**Replace By Id**

```typescript
let replaceById = await g.sys.db.gen.replaceByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: 1,
    updateData: {
        "first_name": "MALLORY"
    }
});
```

## replaceById primaryKey override

**Replace By Id primarykey override**

```typescript
let replaceById = await g.sys.db.gen.replaceByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: "Bob",
    primaryKey: "first_name",    
    updateData: {
        "first_name": "MALLORY"
    }
});
```

## replaceById [header]

**Replace By Id header**

```typescript
let replaceById = await g.sys.db.gen.replaceByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: "Bob",
    primaryKey: "first_name",    
    updateData: {
        "first_name": "MALLORY"
    },
    headers: {
        "x-am-response-case": "capitalCase"
    }
});
```

## replaceById [select]

**Replace By Id select**

```typescript
let replaceById = await g.sys.db.gen.replaceByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: "Bob",
    primaryKey: "first_name",    
    updateData: {
        "first_name": "MALLORY"
    },
    select: "first_name,last_name"
});
```

## replaceById [upsert]

**Replace By Id upsert**

```typescript
let replaceById = await g.sys.db.gen.replaceByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: "Bob",
    primaryKey: "first_name",    
    updateData: {
        "first_name": "MALLORY"
    },
    select: "first_name,last_name",
    upsert: true
});
```

## replaceById [returnDocument]

**Replace By Id returnDocument**

```typescript
let replaceById = await g.sys.db.gen.replaceByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: "Bob",
    primaryKey: "first_name",    
    updateData: {
        "first_name": "MALLORY"
    },
    select: "first_name,last_name",
    returnDocument: true
});
```

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

**Replace the customer 1 of the tenant acme**

```typescript
let replaceById = await g.sys.db.gen.replaceByIdGen({
    instance: "crm::acme", database: "crm", collection: "customers",
    id: 1,
    updateData: {
        "first_name": "MALLORY",
        "last_name": "Lin"
    }
});
```

`g.sys.db.gen.removeByIdGen` calls the [remove by id generated API](https://docs.apimaker.dev/v1/docs/apis-all/generated-apis/auto-generated-remove-by-id-api.html) from your code : no schema, the data as it is in the table.

## Structure

**Remove by id**

```typescript
let removeById = await g.sys.db.gen.removeByIdGen({
    instance: "INSTANCE_NAME", database: 'DB_NAME', collection: "COLLECTION_NAME",
    select: "COLUMN_NAMES",
    id: 7878,
    primaryKey: "COLUMN_NAME"
});
```

## Simple removeById

**Remove By Id**

```typescript
let updateById = await g.sys.db.gen.removeByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: 1,
    updateData: {
        "first_name": "MALLORY"
    }
});
```

## removeById primaryKey override

**Remove By Id primarykey override**

```typescript
let updateById = await g.sys.db.gen.removeByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: "Bob",
    primaryKey: "first_name",    
    updateData: {
        "first_name": "MALLORY"
    }
});
```

## removeById [header]

**Remove By Id header**

```typescript
let updateById = await g.sys.db.gen.removeByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: "Bob",
    primaryKey: "first_name",    
    updateData: {
        "first_name": "MALLORY"
    },
    headers: {
        "x-am-response-case": "capitalCase"
    }
});
```

## removeById [select]

**Remove By Id header**

```typescript
let updateById = await g.sys.db.gen.removeByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: "Bob",
    primaryKey: "first_name",    
    updateData: {
        "first_name": "MALLORY"
    },
    select: "first_name,last_name"
});
```

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

**Remove the customer 101 of the tenant acme**

```typescript
let removeById = await g.sys.db.gen.removeByIdGen({
    instance: "crm::acme", database: "crm", collection: "customers",
    id: 101
});
```

`g.sys.db.gen.removeByQueryGen` calls the [remove by query generated API](https://docs.apimaker.dev/v1/docs/apis-all/generated-apis/auto-generated-remove-by-query-api.html) from your code : no schema, the data as it is in the table.

## Structure

**Remove by query**

```typescript
let updateById = await g.sys.db.gen.removeByQueryGen({
    instance: "INSTANCE_NAME", database: 'DB_NAME', collection: "COLLECTION_NAME",
    find: {
        "city_name": "LONDON"
    }
});
```

## Simple removeByQuery

**Remove by query**

```typescript
let updateById = await g.sys.db.gen.removeByQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "first_name": "Bob"
    }
});
```

## $lt removeByQuery

**$lt Remove by query**

```typescript
let updateById = await g.sys.db.gen.removeByQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        customer_id: {
            "$lt": 2
        }
    }
});
```

## $lte removeByQuery

**$lte Remove by query**

```typescript
let updateById = await g.sys.db.gen.removeByQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        customer_id: {
            "$lte": 2
        }
    }
});
```

## $gt removeByQuery

**$gt Remove by query**

```typescript
let updateById = await g.sys.db.gen.removeByQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        customer_id: {
            "$gt": 2
        }
    }
});
```

## $gte removeByQuery

**$gte Remove by query**

```typescript
let updateById = await g.sys.db.gen.removeByQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        customer_id: {
            "$gte": 2
        }
    }
});
```

## $eq removeByQuery

**$eq Remove by query**

```typescript
let updateById = await g.sys.db.gen.removeByQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        customer_id: {
            "$eq": 2
        }
    }
});
```

## $ne removeByQuery

**$ne Remove by query**

```typescript
let updateById = await g.sys.db.gen.removeByQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        customer_id: {
            "$ne": 2
        }
    }
});
```

## $not removeByQuery

**$not Remove by query**

```typescript
let updateById = await g.sys.db.gen.removeByQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        customer_id: {
            "$not": {
                "$in": [1, 2, 3, 4]
            }
        }
    }
});
```

## $and removeByQuery

**$and Remove by query**

```typescript
let updateById = await g.sys.db.gen.removeByQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "$and": [
            {
                "customer_id": 2
            },
            {
                "pincode": 7171
            }
        ]
    }
});
```

## $or removeByQuery

**$or Remove by query**

```typescript
let updateById = await g.sys.db.gen.removeByQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "$or": [
            {
                "customer_id": 2
            },
            {
                "pincode": 7171
            }
        ]
    }
});
```

## $in removeByQuery

**$in Remove by query**

```typescript
let updateById = await g.sys.db.gen.removeByQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "customer_id": {
            "$in": [1, 2]
        }
    }
});
```

## $nin removeByQuery

**$nin Remove by query**

```typescript
let updateById = await g.sys.db.gen.removeByQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "customer_id": {
            "$nin": [1, 2]
        }
    }
});
```

## $like removeByQuery

**$nin Remove by query**

```typescript
let updateById = await g.sys.db.gen.removeByQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "first_name": {
            "$like": "Bob%"
        }
    }
});
```

## MongoDB $in operator in nested find

**$in operator in nested find**

```typescript
let updateById = await g.sys.db.gen.removeByQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "eventListeners.versions.name": {
            "$in": [
                "Original",
                "1"
            ]
        }
    }
});
```

## MongoDB $nin operator in nested find

**$nin operator in nested find**

```typescript
let updateById = await g.sys.db.gen.removeByQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "eventListeners.versions.name": {
            "$nin": [
                "Original",
                "1"
            ]
        }
    }
});
```

## MongoDB $and operator in nested find

**$and operator in nested find**

```typescript
let updateById = await g.sys.db.gen.removeByQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "$and": [
            {
                "eventListeners.versions.name": "Original"
            },
            {
                "eventListeners.versions.version": "-1"
            }
        ]
    }
});
```

## MongoDB $or operator in nested find

**$or operator in nested find**

```typescript
let updateById = await g.sys.db.gen.removeByQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "$or": [
            {
                "eventListeners.versions.name": "Original"
            },
            {
                "eventListeners.versions.version": "-1"
            }
        ]
    }
});
```

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

**Remove customers of the tenant acme by query**

```typescript
let removeByQuery = await g.sys.db.gen.removeByQueryGen({
    instance: "crm::acme", database: "crm", collection: "customers",
    find: {
        "first_name": "Bob"
    }
});
```

`g.sys.db.gen.queryGen` calls the [query for get data generated API](https://docs.apimaker.dev/v1/docs/apis-all/generated-apis/auto-generated-query-for-get-data-api.html) from your code : no schema, the data as it is in the table.

## Structure

**Query method**

```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "INSTANCE_NAME", database: 'DB_NAME', collection: "COLLECTION_NAME",
    find: {
        "pincode": 100050
    },
    limit: 1,
    select: "COLUMN_NAMES",
    deep: [{
        s_key: "SOURCE_COLLECTION_COLUMN_NAME",
        t_key: "TARGET_COLLECTION_COLUMN_NAME",
        t_col: "TARGET_COLLECTION_NAME",
        select: "COLUMN_NAMES"
    }],
    skip: 1,
    sort: "COLUMN_NAME",
    groupBy: "COLUMN_NAME"
});
```

## Simple query
```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find:{
        "first_name": "Bob"
    }
});
```

## Simple [headers]

**headers**

```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find:{
        "first_name": "Bob"
    },
    headers: {
        "x-am-response-case": "capitalCase"
    }
});
```

## Simple [select]

**select**

```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find:{
        "first_name": "Bob"
    },
    select: "first_name"
});
```

## Simple [limit]

**limit**

```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find:{
        "first_name": "Bob"
    },
    limit: 2
});
```

## Simple [skip]

**skip**

```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find:{
        "first_name": "Bob"
    },
    skip: 1
});
```

## Simple [sort]

**sort**

```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find:{
        "first_name": "Bob"
    },
    sort: "first_name"
});
```

## Simple [deep]

**deep**

```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find:{
        "first_name": "Bob"
    },
    deep: [
        {
            s_key: "customer_id",
            t_col: "products",
            t_key: "owner_id",
            select: "customer_id,first_name"
        }
    ]
});
```

## $lt query
```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        customer_id: {
            "$lt": 2
        }
    }
});
```

## $lte query
```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        customer_id: {
            "$lte": 2
        }
    }
});
```

## $gt query
```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        customer_id: {
            "$gt": 2
        }
    }
});
```

## $gte query
```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        customer_id: {
            "$gte": 2
        }
    }
});
```

## $eq query
```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        customer_id: {
            "$eq": 2
        }
    }
});
```

## $ne query
```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        customer_id: {
            "$ne": 2
        }
    }
});
```

## $not query
```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        customer_id: {
            "$not": {
                "$in": [1, 2, 3, 4]
            }
        }
    }
});
```

## $and query
```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "$and": [
            {
                "customer_id": 2
            },
            {
                "pincode": 7171
            }
        ]
    }
});
```

## $or query
```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "$or": [
            {
                "customer_id": 2
            },
            {
                "pincode": 7171
            }
        ]
    }
});
```

## $in query
```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "customer_id": {
            "$in": [1, 2]
        }
    }
});
```

## $nin query
```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "customer_id": {
            "$nin": [1, 2]
        }
    }
});
```

## $like query
```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "first_name": {
            "$like": "Bob%"
        }
    }
});
```

## MongoDB $in operator in nested find

**$in operator in nested find**

```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "eventListeners.versions.name": {
            "$in": [
                "Original",
                "1"
            ]
        }
    }
});
```

## MongoDB $nin operator in nested find

**$nin operator in nested find**

```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "eventListeners.versions.name": {
            "$nin": [
                "Original",
                "1"
            ]
        }
    }
});
```

## MongoDB $and operator in nested find

**$and operator in nested find**

```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "$and": [
            {
                "eventListeners.versions.name": "Original"
            },
            {
                "eventListeners.versions.version": "-1"
            }
        ]
    }
});
```

## MongoDB $or operator in nested find

**$or operator in nested find**

```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "$or": [
            {
                "eventListeners.versions.name": "Original"
            },
            {
                "eventListeners.versions.version": "-1"
            }
        ]
    }
});
```

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

**Query the customers of the tenant acme**

```typescript
let query = await g.sys.db.gen.queryGen({
    instance: "crm::acme", database: "crm", collection: "customers",
    find: {
        "first_name": "Bob"
    },
    sort: "customer_id",
    limit: 10
});
```

`g.sys.db.gen.queryByStreamGen` calls the [query for get data by stream generated API](https://docs.apimaker.dev/v1/docs/apis-all/generated-apis/auto-generated-query-for-get-data-by-stream-api.html) from your code : no schema, the data as it is in the table.

## Structure

**Query by stream**

```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "INSTANCE_NAME", database: 'DB_NAME', collection: "COLLECTION_NAME",
    find: {
        "pincode": 100050
    },
    limit: 1,
    select: "COLUMN_NAMES",
    deep: [{
        s_key: "SOURCE_COLLECTION_COLUMN_NAME",
        t_key: "TARGET_COLLECTION_COLUMN_NAME",
        t_col: "TARGET_COLLECTION_NAME",
        select: "COLUMN_NAMES"
    }],
    skip: 1,
    sort: "COLUMN_NAME",
    groupBy: "COLUMN_NAME"
}, (obj) => {
    response.push(obj);
});
```

## Simple queryByStream
```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "first_name": "Bob"
    }
}, (obj) => {
    response.push(obj);
});
```

## Simple [headers]

**headers**

```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "first_name": "Bob"
    },
    headers: {
        "x-am-response-case": "capitalCase"
    }
}, (obj) => {
    response.push(obj);
});
```

## Simple [select]

**select**

```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "first_name": "Bob"
    },
    select: "first_name"
}, (obj) => {
    response.push(obj);
});
```

## Simple [limit]

**limit**

```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "first_name": "Bob"
    },
    limit: 2
}, (obj) => {
    response.push(obj);
});
```

## Simple [skip]

**skip**

```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "first_name": "Bob"
    },
    skip: 1
}, (obj) => {
    response.push(obj);
});
```

## Simple [sort]

**sort**

```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "first_name": "Bob"
    },
    sort: "first_name"
}, (obj) => {
    response.push(obj);
});
```

## Simple [deep]

**deep**

```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "first_name": "Bob"
    },
    deep: [
        {
            s_key: "customer_id",
            t_col: "products",
            t_key: "owner_id",
            select: "customer_id,first_name"
        }
    ]
}, (obj) => {
    response.push(obj);
});
```

## $lt queryByStream
```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        customer_id: {
            "$lt": 2
        }
    }
}, (obj) => {
    response.push(obj);
});
```

## $lte queryByStream
```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        customer_id: {
            "$lte": 2
        }
    }
}, (obj) => {
    response.push(obj);
});
```

## $gt queryByStream
```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        customer_id: {
            "$gt": 2
        }
    }
}, (obj) => {
    response.push(obj);
});
```

## $gte queryByStream
```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        customer_id: {
            "$gte": 2
        }
    }
}, (obj) => {
    response.push(obj);
});
```

## $eq queryByStream
```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        customer_id: {
            "$eq": 2
        }
    }
}, (obj) => {
    response.push(obj);
});
```

## $ne queryByStream
```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        customer_id: {
            "$ne": 2
        }
    }
}, (obj) => {
    response.push(obj);
});
```

## $not queryByStream
```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        customer_id: {
            "$not": {
                "$in": [1, 2, 3, 4]
            }
        }
    }
}, (obj) => {
    response.push(obj);
});
```

## $and queryByStream
```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "$and": [
            {
                "customer_id": 2
            },
            {
                "pincode": 7171
            }
        ]
    }
}, (obj) => {
    response.push(obj);
});
```

## $or queryByStream
```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "$or": [
            {
                "customer_id": 2
            },
            {
                "pincode": 7171
            }
        ]
    }
}, (obj) => {
    response.push(obj);
});
```

## $in queryByStream
```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "customer_id": {
            "$in": [1, 2]
        }
    }
}, (obj) => {
    response.push(obj);
});
```

## $nin queryByStream
```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "customer_id": {
            "$nin": [1, 2]
        }
    }
}, (obj) => {
    response.push(obj);
});
```

## $like queryByStream
```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "first_name": {
            "$like": "Bob%"
        }
    }
}, (obj) => {
    response.push(obj);
});
```

## MongoDB $in operator in nested find

**$in operator in nested find**

```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "eventListeners.versions.name": {
            "$in": [
                "Original",
                "1"
            ]
        }
    }
}, (obj) => {
    response.push(obj);
});
```

## MongoDB $nin operator in nested find

**$nin operator in nested find**

```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "eventListeners.versions.name": {
            "$nin": [
                "Original",
                "1"
            ]
        }
    }
}, (obj) => {
    response.push(obj);
});
```

## MongoDB $and operator in nested find

**$and operator in nested find**

```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "$and": [
            {
                "eventListeners.versions.name": "Original"
            },
            {
                "eventListeners.versions.version": "-1"
            }
        ]
    }
}, (obj) => {
    response.push(obj);
});
```

## MongoDB $or operator in nested find

**$or operator in nested find**

```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "$or": [
            {
                "eventListeners.versions.name": "Original"
            },
            {
                "eventListeners.versions.version": "-1"
            }
        ]
    }
}, (obj) => {
    response.push(obj);
});
```

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

**Query the customers of the tenant acme by stream**

```typescript
let response = [];
await g.sys.db.gen.queryByStreamGen({
    instance: "crm::acme", database: "crm", collection: "customers",
    find: {
        "first_name": "Bob"
    }
}, (obj) => {
    response.push(obj);
});
```

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

## Structure

**Aggregate method**

```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "INSTANCE_NAME", database: 'DB_NAME', collection: "COLLECTION_NAME",
    aggregateQuery: [
        {
            "$match":
                {
                    "first_name": "JENNIFER"
                }
        }
    ]
});
```

## MongoDB Simple aggregate
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[{
        "$limit": 2
    }]
});
```

## MongoDB headers
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[{
        "$limit": 2
    }],
    headers: {
        "x-am-authorization": "TOKEN"
    }
});
```

## MongoDB Basic aggregation
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[[
        {
            "$group": {
                "_id": "$_id",
                "total": {
                    "$sum": "$customer_id"
                }
            }
        },
        {
            "$match": {
                "total": {
                    "$gt": 1
                }
            }
        },
        {
            "$sort": {
                "total": -1
            }
        },
        {
            "$count": "CountValue"
        }
    ]]
});
```

## MongoDB $project stage
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "full_name": {
                    "$toLower": "$first_name"
                }
            }
        }
    ]
});
```

## MongoDB $addFields
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$addFields": {
                "full_name": {
                    "$concat": [
                        "$first_name",
                        " ",
                        "$last_name"
                    ]
                }
            }
        }
    ]
});
```

## MongoDB $bucket
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$bucket": {
                "groupBy": "$price",
                "boundaries": [10000,20000,30000,40000,50000],
                "default": "Other",
                "output": {
                    "count": {
                        "$sum": 1
                    },
                    "product_details": {
                        "$push": {
                            "name": "$name",
                            "price": "$price"
                        }
                    }
                }
            }
        }
    ]
});
```

## MongoDB $facet with multiple $bucket
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$facet": {
                "price": [
                    {
                        "$bucket": {
                            "groupBy": "$price",
                            "boundaries": [10000,20000,30000,40000,50000,60000,70000,80000],
                            "default": "Other",
                            "output": {
                                "count": {
                                    "$sum": 1
                                },
                                "product_details": {
                                    "$push": {
                                        "name": "$name",
                                        "price": "$price"
                                    }
                                }
                            }
                        }
                    }
                ],
                "categories": [
                    {
                        "$bucket": {
                            "groupBy": "$category_id",
                            "boundaries": [10000,20000,30000,40000,50000,60000,70000,80000],
                            "default": "Other",
                            "output": {
                                "count": {
                                    "$sum": 1
                                },
                                "product_details": {
                                    "$push": {
                                        "name": "$name",
                                        "id": "$id",
                                        "category_id": "$category_id"
                                    }
                                }
                            }
                        }
                    }
                ]
            }
        }
    ]
});
```

## MongoDB $collStats, latencyStats > histograms, storageStats, count
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$collStats": {
                "latencyStats": {
                    "histograms": true
                },
                "storageStats": {},
                "count": {}
            }
        }
    ]
});
```

## MongoDB $collStats, latencyStats > histograms, storageStats, count
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$collStats": {
                "latencyStats": {
                    "histograms": true
                },
                "storageStats": {},
                "count": {}
            }
        }
    ]
});
```

## MongoDB $count
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$match": {
                "price": {
                    "$gt": 10000
                }
            }
        },
        {
            "$count": "price_gt_10000"
        }
    ]
});
```

## MongoDB $limit
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$limit": 2
        }
    ]
});
```

## MongoDB $lookup
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$lookup": {
                "from": "products",
                "localField": "customer_id",
                "foreignField": "owner_id",
                "as": "customer_data"
            }
        }
    ]
});
```

## MongoDB $match
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$match": {
                "first_name": "Bob"
            }
        }
    ]
});
```

## MongoDB $redact, $cond, if, then, else, $$DESCEND, $$PRUNE
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$redact": {
                "$cond": {
                    "if": {
                        "$eq": [
                            "$first_name",
                            "JOHNNY"
                        ]
                    },
                    "then": "$$DESCEND",
                    "else": "$$PRUNE"
                }
            }
        }
    ]
});
```

## MongoDB $replaceRoot, newRoot
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$replaceRoot": {
                "newRoot": {
                    "full_name": {
                        "$concat": [
                            "$first_name",
                            " ",
                            "$last_name"
                        ]
                    }
                }
            }
        }
    ]
});
```

## MongoDB $sample
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$sample": {
                "size": 2
            }
        }
    ]
});
```

## MongoDB $skip
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$skip": 2
        }
    ]
});
```

## MongoDB $sort
### ascending

**MongoDB $sort**

```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$sort": {
                "customer_id": 1
            }
        }
    ]
});
```
### descending

**MongoDB $sort**

```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$sort": {
                "customer_id": -1
            }
        }
    ]
});
```

## MongoDB $unwind, $sortByCount
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$unwind": "$ages"
        },
        {
            "$sortByCount": "$ages"
        }
    ]
});
```

## MongoDB $abs, $subtract
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "Grand_Total": {
                    "$abs": {
                        "$subtract": [
                            "$total",
                            "$tax"
                        ]
                    }
                }
            }
        }
    ]
});
```

## MongoDB $add
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "Grand_Total": {
                    "$add": [
                        "$price",
                        "$category_id"
                    ]
                }
            }
        }
    ]
});
```

## MongoDB $ceil, $floor
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "ceilingValue": {
                    "$ceil": "$price"
                },
                "floorValue": {
                    "$floor": "$price"
                },
                "id": 1
            }
        }
    ]
});
```

## MongoDB $divide
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "pricePercentage": {
                    "$divide": [
                        "$price",
                        100
                    ]
                },
                "id": 1
            }
        }
    ]
});
```

## MongoDB $multiply
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "Grand_Total": {
                    "$multiply": [
                        "$price",
                        "$quantity"
                    ]
                },
                "id": 1
            }
        }
    ]
});
```

## MongoDB $arrayElemAt
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
          "$project": {
            "first": {
              "$arrayElemAt": [
                "$attribuites.color",
                1
              ]
            }
          }
        }
    ]
});
```

## MongoDB $filter
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
          "$project": {
            "ages": {
              "$filter": {
                "input": "$ages",
                "as": "age",
                "cond": {
                  "$gte": [
                    "$$age.age",
                    50
                  ]
                }
              }
            }
          }
        }
    ]
});
```

## MongoDB $in
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "customer_id": "$customer_id",
                "TravelingHobby": {
                    "$in": [
                        "traveling",
                        "$hobbies"
                    ]
                }
            }
        }
    ]
});
```

## MongoDB $indexOfArray
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "customer_id": "$customer_id",
                "index": {
                    "$indexOfArray": [
                        "$ages.birth_year",
                        1971
                    ]
                }
            }
        }
    ]
});
```

## MongoDB $isArray
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "isHobbyCricket": {
                    "$cond": {
                        "if": {
                            "$isArray": "$hobbies"
                        },
                        "then": {
                            "$in": [
                                "traveling",
                                "$hobbies"
                            ]
                        },
                        "else": "One or more fields is not an array."
                    }
                }
            }
        }
    ]
});
```

## MongoDB $map
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "integerValues": {
                    "$map": {
                        "input": "$distances",
                        "as": "decimalValue",
                        "in": {
                            "$trunc": "$$decimalValue"
                        }
                    }
                }
            }
        }
    ]
});
```

## MongoDB $reverseArray
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "reverseDistances": {
                    "$reverseArray": "$distances"
                }
            }
        }
    ]
});
```

## MongoDB $size
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "customer_id": 1,
                "address": 1,
                "numberOfHobbies": {
                    "$cond": {
                        "if": {
                            "$isArray": "$hobbies"
                        },
                        "then": {
                            "$size": "$hobbies"
                        },
                        "else": "NA"
                    }
                }
            }
        }
    ]
});
```

## MongoDB $and
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": "$id",
                "result": {
                    "$and": [
                        {
                            "$gt": [
                                "$price",
                                60000
                            ]
                        },
                        {
                            "$lt": [
                                "$category_id",
                                45000
                            ]
                        }
                    ]
                }
            }
        }
    ]
});
```

## MongoDB $not
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": "$id",
                "result": {
                    "$not": [
                        {
                            "$gt": [
                                "$price",
                                30000
                            ]
                        }
                    ]
                }
            }
        }
    ]
});
```

## MongoDB $or
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": "$id",
                "result": {
                    "$or": [
                        {
                            "$gt": [
                                "$price",
                                60000
                            ]
                        },
                        {
                            "$lt": [
                                "$category_id",
                                1000
                            ]
                        }
                    ]
                }
            }
        }
    ]
});
```

## MongoDB $cmp
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": 1,
                "comparePrice": {
                    "$cmp": [
                        "$price",
                        42187
                    ]
                },
                "_id": 1
            }
        }
    ]
});
```

## MongoDB $eq
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": 1,
                "equalPrice": {
                    "$eq": [
                        "$price",
                        42187
                    ]
                },
                "_id": 0
            }
        }
    ]
});
```

## MongoDB $gt
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": 1,
                "graterPrice": {
                    "$gt": [
                        "$price",
                        42187
                    ]
                },
                "_id": 0
            }
        }
    ]
});
```

## MongoDB $gte
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": 1,
                "gePrice": {
                    "$gte": [
                        "$price",
                        42187
                    ]
                },
                "_id": 0
            }
        }
    ]
});
```

## MongoDB $lt
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": 1,
                "graterPrice": {
                    "$lt": [
                        "$price",
                        42187
                    ]
                },
                "_id": 0
            }
        }
    ]
});
```

## MongoDB $lte
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": 1,
                "gePrice": {
                    "$lte": [
                        "$price",
                        42187
                    ]
                },
                "_id": 0
            }
        }
    ]
});
```

## MongoDB $ne
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": 1,
                "nePrice": {
                    "$ne": [
                        "$price",
                        42187
                    ]
                },
                "_id": 0
            }
        }
    ]
});
```

## MongoDB $cond
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": 1,
                "price": 1,
                "discount": {
                    "$cond": {
                        "if": {
                            "$gt": [
                                "$price",
                                50000
                            ]
                        },
                        "then": 30,
                        "else": 10
                    }
                },
                "_id": 0
            }
        }
    ]
});
```

## MongoDB $ifNull
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": 1,
                "price": 1,
                "shipping": {
                    "$ifNull": [
                        "$shipping",
                        null
                    ]
                },
                "_id": 0
            }
        }
    ]
});
```

## MongoDB $switch
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": 1,
                "price": 1,
                "priceSummary": {
                    "$switch": {
                        "branches": [
                            {
                                "case": {
                                    "$eq": [
                                        "$price",
                                        50000
                                    ]
                                },
                                "then": "equals"
                            },
                            {
                                "case": {
                                    "$gt": [
                                        "$price",
                                        50000
                                    ]
                                },
                                "then": "greater than"
                            },
                            {
                                "case": {
                                    "$lt": [
                                        "$price",
                                        50000
                                    ]
                                },
                                "then": "less than"
                            }
                        ]
                    }
                }
            }
        }
    ]
});
```

## MongoDB $concat
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": 1,
                "name_Description": {
                    "$concat": [
                        "$name",
                        "_",
                        "$description"
                    ]
                }
            }
        }
    ]
});
```

## MongoDB $max
```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$group": {
                "_id": "$price",
                "maxQuantity": {
                    "$max": "$quantity"
                }
            }
        }
    ]
});
```

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

**Aggregate the customers of the tenant acme (MongoDB)**

```typescript
let aggregate = await g.sys.db.gen.aggregateGen({
    instance: "crm::acme", database: "crm", collection: "customers",
    aggregateQuery: [{
        "$group": {
            "_id": "$city",
            "customers": { "$sum": 1 }
        }
    }]
});
```

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

## Structure

**Count method**

```typescript
let count = await g.sys.db.gen.countGen({
    instance: "INSTANCE_NAME", database: 'DB_NAME', collection: "COLLECTION_NAME",
    find: {
        "pincode": 100050
    }
});
```

## Simple get count
```typescript
let count = await g.sys.db.gen.countGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers"
});
```

## Simple headers
```typescript
let count = await g.sys.db.gen.countGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    headers: {
        "x-am-response-case": "capitalCase"
    }
});
```

## Simple find
```typescript
let count = await g.sys.db.gen.countGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find:{
        "first_name": "Bob"
    }
});
```

## $gt
```typescript
let count = await g.sys.db.gen.countGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "isActive": {
            "$gt": 4
        }
    }
});
```

## $gte
```typescript
let count = await g.sys.db.gen.countGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "isActive": {
            "$gte": 4
        }
    }
});
```

## $lt
```typescript
let count = await g.sys.db.gen.countGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "isActive": {
            "$lt": 4
        }
    }
});
```

## $lte
```typescript
let count = await g.sys.db.gen.countGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "isActive": {
            "$lte": 4
        }
    }
});
```

## $eq
```typescript
let count = await g.sys.db.gen.countGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "first_name": {
            "$eq": "Bob"
        }
    }
});
```

## $ne
```typescript
let count = await g.sys.db.gen.countGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "first_name": {
            "$ne": "Bob"
        }
    }
});
```

## $in
```typescript
let count = await g.sys.db.gen.countGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "isActive": {
            "$in": [1, 2]
        }
    }
});
```

## $nin
```typescript
let count = await g.sys.db.gen.countGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "isActive": {
            "$nin": [1, 2]
        }
    }
});
```

## $and
```typescript
let count = await g.sys.db.gen.countGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "$and": [
            {
                "employee_Details.emp_Id": 1
            },
            {
                "city": "Baroda"
            }
        ]
    }
});
```

## $or
```typescript
let count = await g.sys.db.gen.countGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "$or": [
            {
                "employee_Details.emp_Id": 1
            },
            {
                "city": "Baroda"
            }
        ]
    }
});
```

## $like
```typescript
let count = await g.sys.db.gen.countGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "first_name": {
            "$like": "%Mall%"
        }
    }
});
```

## % after text
```typescript
let count = await g.sys.db.gen.countGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "first_name": {
            "$like": "Mall%"
        }
    }
});
```

## count with multiple column
```typescript
let count = await g.sys.db.gen.countGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "first_name": "Mallory",
        "last_name": "Brown"
    }
});
```

## count with multiple column
```typescript
let count = await g.sys.db.gen.countGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "first_name": "Mallory",
        "last_name": "Brown"
    }
});
```

## floating value support
```typescript
let count = await g.sys.db.gen.countGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    find: {
        "isActive":{
            "$gt": 2.2
        }
    }
});
```

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

**Count the customers of the tenant acme**

```typescript
let count = await g.sys.db.gen.countGen({
    instance: "crm::acme", database: "crm", collection: "customers",
    find: {
        "first_name": "Bob"
    }
});
```

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

## Structure

**Distinct method**

```typescript
let distinct = await g.sys.db.gen.distinctGen({
    instance: "INSTANCE_NAME", database: 'DB_NAME', collection: "COLLECTION_NAME",
    distinctField: "COLUMN_NAME",
    order: "asc"
});
```

## Simple distinct
```typescript
let distinct = await g.sys.db.gen.distinctGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name"
});
```

## distinct with header
```typescript
let distinct = await g.sys.db.gen.distinctGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    headers: {
        "x-am-response-case": "capitalCase"
    }
});
```

## distinct with order
### ascending

**distinct with order**

```typescript
let distinct = await g.sys.db.gen.distinctGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    order: "asc"
});
```
### descending

**distinct with header**

```typescript
let distinct = await g.sys.db.gen.distinctGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    order: "desc"
});
```

## distinct with headers and order

**distinct with header & order**

```typescript
let distinct = await g.sys.db.gen.distinctGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    headers: {
        "x-am-response-case": "capitalCase"
    },
    order: "asc"
});
```

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

**Distinct first names of the customers of the tenant acme**

```typescript
let distinct = await g.sys.db.gen.distinctGen({
    instance: "crm::acme", database: "crm", collection: "customers",
    distinctField: "first_name"
});
```

`g.sys.db.gen.distinctQueryGen` calls the [distinct with query generated API](https://docs.apimaker.dev/v1/docs/apis-all/generated-apis/auto-generated-distinct-with-query-api.html) from your code : no schema, the data as it is in the table.

## Structure

**Distinct Query**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "INSTANCE_NAME", database: 'DB_NAME', collection: "COLLECTION_NAME",
    distinctField: "COLUMN_NAME",
    order: "asc",
    find: {
        "price": 42187
    }
});
```

## Simple get distinctWithQuery

**Simple distinctWithQuery**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name"
});
```

## distinctWithQuery with multiple distinctField
```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name,last_name"
});
```

## distinctWithQuery with headers
```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    headers: {
        "x-am-response-case": "capitalCase"
    }
});
```

## distinctWithQuery with order
### ascending

**distinctWithQuery with ascending order**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    order: "asc"
});
```
### descending

**distinctWithQuery with descending order**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    order: "desc"
});
```

## distinctWithQuery with headers and order

**distinctWithQuery with headers**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    headers: {
        "x-am-response-case": "capitalCase"
    },
    order: "asc"
});
```

## distinctWithQuery with find
```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        first_name: "Bob"
    }
});
```

## $lt distinctWithQuery

**distinctWithQuery with $lt**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        customer_id: {
            "$lt": 2
        }
    }
});
```

## $lte distinctWithQuery

**distinctWithQuery with $lte**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        customer_id: {
            "$lte": 2
        }
    }
});
```

## $gt distinctWithQuery

**distinctWithQuery with $gt**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        customer_id: {
            "$gt": 2
        }
    }
});
```

## $gte distinctWithQuery

**distinctWithQuery with $gte**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        customer_id: {
            "$gte": 2
        }
    }
});
```

## $eq distinctWithQuery

**distinctWithQuery with $eq**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        customer_id: {
            "$eq": 2
        }
    }
});
```

## $ne distinctWithQuery

**distinctWithQuery with $ne**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        customer_id: {
            "$ne": 2
        }
    }
});
```

## $not distinctWithQuery

**distinctWithQuery with $not**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        customer_id: {
            "$not": {
                "$in": [1,2,3]
            }
        }
    }
});
```

## $and distinctWithQuery

**distinctWithQuery with $and**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        "$and": [
            {
                "customer_id": 1
            },
            {
                "pincode": 382345
            }
        ]
    }
});
```

## $or distinctWithQuery

**distinctWithQuery with $or**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        "$or": [
            {
                "customer_id": 1
            },
            {
                "pincode": 382345
            }
        ]
    }
});
```

## $in distinctWithQuery

**distinctWithQuery with $in**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        customer_id: {
            "$in": [1, 2,3]
        }
    }
});
```

## $nin distinctWithQuery

**distinctWithQuery with $nin**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        customer_id: {
            "$nin": [1, 2,3]
        }
    }
});
```

## $like distinctWithQuery

**distinctWithQuery with $like**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        first_name: {
            "$like": "Bob%"
        }
    }
});
```

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

**Distinct first names of some customers of the tenant acme**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "crm::acme", database: "crm", collection: "customers",
    distinctField: "first_name",
    find: {
        "last_name": "Lin"
    }
});
```
