# Query for Get Data

> Perform complex queries to retrieve data from a collection with filtering, sorting, and pagination using API Maker.

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

## Simple query

**Simple query to get data**

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

## query [headers]

**query headers**

```typescript
let query = await g.sys.db.query({
    instance: "mysql_8", database: "inventory", collection: "customers",
    find:{
        "first_name": "Bob"
    },
    headers: {
        "x-am-response-case": "capitalCase", // noChange | camelCase | capitalCase | constantCase | dotCase | headerCase | noCase | paramCase | pascalCase | pathCase | sentenceCase | snakeCase
    },
});
```

## query [select]

**query select**

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

## query [limit]

**query limit**

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

## query [skip]

**query skip**

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

## query [sort]

**query sort**

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

## query [deep]

**query deep**

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

## $lt [less than] query

**less than query**

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

## $lte [less than equal] query

**less than equal query**

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

## $gt [greater than] query

**greater than query**

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

## $gte [greater than equal] query

**greater than equal query**

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

## $eq [equal to] query

**$eq query**

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

## $ne [not equal to] query

**$ne query**

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

## $not [not] query

**$not query**

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

## $and [and] query

**$and query**

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

Find-join example

**find join $and query**

```typescript
let query = await g.sys.db.query({
    instance: "mysql_8", database: "inventory", collection: "customers",
    find: {
        "$and": [
            {
                "first_name.customer_id.shipping_id": 2
            },
            {
                "first_name.customer_id.order_id": 58
            }
        ]
    }
});
```

## $or [or] query

**$or query**

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

Find-join example

**find join $or query**

```typescript
let query = await g.sys.db.query({
    instance: "mysql_8", database: "inventory", collection: "customers",
    find: {
        "$or": [
            {
                "first_name.customer_id.shipping_id": 2
            },
            {
                "first_name.customer_id.order_id": 58
            }
        ]
    }
});
```

## $in [in] query

**$in query**

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

## $nin [not in] query

**$nin query**

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

## $like [like] query

**$like query**

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

## $isNull [null check] with sql databases

**$isNull use**

```typescript
let query = await g.sys.db.query({
    instance: "mysql_8", database: "inventory", collection: "customers",
    find: {
        "first_name": {
            "$isNull": true
        }
    }
});
```

## MongoDB $in operator in nested find

**$in operator in nested find**

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

## Structure

**query with all params **

```typescript
let query = await g.sys.db.query({
    instance: "INSTANCE_NAME", database: 'DB_NAME', collection: "COLLECTION_NAME",
    find: {
        "COLUMN_NAME": "VALUE"
    },
    limit: 10,
    select: "COMMA_SEPARATED_COLUMN_NAMES",
    deep: [
        {
            s_key: "SOURCE_COLLECTION_COLUMN_NAME",
            t_instance: "TARGET_INSTANCE_NAME",
            t_db: "TARGET_DATABASE_NAME",
            t_col: "TARGET_COLLECTION_NAME",
            t_key: "TARGET_COLLECTION_COLUMN_NAME",
            find: {
                "COLUMN_NAME": "COLUMN_VALUE"
            },
            isMultiple: true,
            limit: 10,
            select: "COMMA_SEPARATED_COLUMN_NAMES",
            skip: 2,
            sort: "COLUMN_NAME",
        }
    ],
    skip: 1,
    sort: "COLUMN_NAME",
    groupBy: "COLUMN_NAME",
    getTotalCount: true,
    headers: {
        "x-am-response-case": "capitalCase", // noChange | camelCase | capitalCase | constantCase | dotCase | headerCase | noCase | paramCase | pascalCase | pathCase | sentenceCase | snakeCase
        "x-am-response-object-type": "make_flat", // no_action | make_flat
        "x-am-meta": "true", // true, false
        "x-am-internationalization": "USER_I18N_ID",
        "x-am-run-in-sandbox": "2",
        "x-am-content-type-response": "text/xml", // application/json | text/xml | text/yaml | text/plain | text/html | application/octet-stream
        "x-am-cache-control": "reset_cache", // no_action | reset_cache
        "x-am-get-encrypted-data": "get_only_encryption", // no_encryption | get_only_encryption | get_data_and_encryption
        "x-am-sandbox-timeout": "13000",
        "x-no-compression": "true", // true | false
        "x-am-encrypted-payload": "true", // user will send 'true', when payload is encrypted for transfer
        // Multi-tenant : the tenant of the request, each instance finds it in its own tenants table
        "x-am-tenant-username": "TENANT_USERNAME", // ex: acme
        // Authorization headers
        "x-am-authorization": "AUTHORIZATION_TOKEN",
        "x-am-user-authorization": "API_USER_TOKEN",
        "x-aws-authorization": "AWS_TOKEN",
        "x-google-authorization": "GOOGLE_TOKEN",
        "x-azure-authorization": "AZURE_TOKEN",
    },
}, 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.

**Query the customers of the tenant acme**

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