# Record Count API

> Get accurate counts of records matching specific criteria—supporting reporting and analytics.

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

`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"
    }
});
```

## Related

- [Count generated API](https://docs.apimaker.dev/v1/docs/apis-all/generated-apis/auto-generated-count-api.html) · [Schema API from code](https://docs.apimaker.dev/v1/examples/sys/db/count.html) · [The global object g](https://docs.apimaker.dev/v1/docs/pre-defined-terms/global-object-g.html)
