Count

Get the amount of records in a collection.

When you want to display how many records your collection has without displaying them. You can pass filter and paging parameters to filter records upfront and get the amount of filtered records.

[Count] - Get the amount of records in a collection.

get

Sometimes it's useful to have the capability to get records amount in the collection without calling the find function. You can pass filter and paging parameters to filter records upfront and get the amount of filtered records. https://docs.codemash.io/api/database/collections/count

Authorizations
Path parameters
CollectionNamestringRequired

Collection name - unique, lowercased, collection name without whitespace. E.g., if your collection title you have entered in the CodeMash dashboard is "Business Trips" then collection name would be "business-trips".

versionstringRequired

The CodeMash API version used to fetch data from the API. If not specified, the last version will be used. E.g.: v2

Query parameters
FilterstringOptional

Filter cannot be passed as query string, use POST method instead.

Limitinteger · int32Optional

A limit on the number of objects to be returned, between 1 and 100.

Skipinteger · int32Optional

The number of records to skip before counting

Header parameters
AcceptanyRequired

Accept Header

X-CM-ClusterstringOptional

API key of your cluster. Can be passed in a header as X-CM-Cluster.

X-CM-ProjectIdstringRequired

ID of your project. Can be passed in a header as X-CM-ProjectId.

CultureCodestringOptional

Specify culture code when your response from the API should be localised. E.g.: en

Responses
200
Success
application/json
get
GET //{version}/db/{CollectionName}/count HTTP/1.1
Host: api.codemash.io
Authorization: YOUR_API_KEY
X-CM-ProjectId: text
Accept: */*
200

Success

{
  "response_status": {
    "error_code": "text",
    "message": "text",
    "stack_trace": "text",
    "errors": [
      {
        "error_code": "text",
        "field_name": "text",
        "message": "text",
        "meta": {
          "ANY_ADDITIONAL_PROPERTY": "text"
        }
      }
    ],
    "meta": {
      "ANY_ADDITIONAL_PROPERTY": "text"
    }
  },
  "result": 1
}

[Count] - Get the amount of records in a collection.

post

Sometimes it's useful to have the capability to get records amount in the collection without calling the find function. You can pass filter and paging parameters to filter records upfront and get the amount of filtered records. https://docs.codemash.io/api/database/collections/count

Authorizations
Path parameters
CollectionNamestringRequired

Collection name - unique, lowercased, collection name without whitespace. E.g., if your collection title you have entered in the CodeMash dashboard is "Business Trips" then collection name would be "business-trips".

versionstringRequired

The CodeMash API version used to fetch data from the API. If not specified, the last version will be used. E.g.: v2

Header parameters
AcceptanyRequired

Accept Header

X-CM-ClusterstringOptional

API key of your cluster. Can be passed in a header as X-CM-Cluster.

X-CM-ProjectIdstringRequired

ID of your project. Can be passed in a header as X-CM-ProjectId.

CultureCodestringOptional

Specify culture code when your response from the API should be localised. E.g.: en

Body
FilterstringOptional

A query (in JSON format) that specifies which records to count

LimitintegerOptional

A limit on the number of objects to be returned, between 1 and 100.

SkipintegerOptional

The number of records to skip before counting

Responses
200
Success
application/json
post
POST //{version}/db/{CollectionName}/count HTTP/1.1
Host: api.codemash.io
Authorization: YOUR_API_KEY
X-CM-ProjectId: text
Content-Type: application/x-www-form-urlencoded
Accept: */*
Content-Length: 36

"Filter='text'&Limit=1&Skip=1"
200

Success

{
  "response_status": {
    "error_code": "text",
    "message": "text",
    "stack_trace": "text",
    "errors": [
      {
        "error_code": "text",
        "field_name": "text",
        "message": "text",
        "meta": {
          "ANY_ADDITIONAL_PROPERTY": "text"
        }
      }
    ],
    "meta": {
      "ANY_ADDITIONAL_PROPERTY": "text"
    }
  },
  "result": 1
}

You register employees from when to when s/he wants to travel, in which country, and you can attach documents related to the business trip. You can use our templates or create collections and taxonomies manually by following our instructions.

Prerequisites:

  1. Create Employees collection and other related taxonomies from the template. See instructions here.

  2. Create a collection of "Business Trips." You can do it manually or just copy-paste JSON and UI schemas into CodeMash Dashboard. See instructions here. The schema itself can be found here.

Let's say you have some records inserted in the "Business Trips" collection.

Let's get all the amount of records in the collection.

curl -X 'GET' \
  'https://api.codemash.io/v2/db/business-trips/count' \
  -H 'Authorization: Bearer ***' \
  -H 'X-CM-ProjectId: e1aa5e3b-f16d-4f43-a315-2d89f7633dc3' \
  -H 'Accept: application/json'

Let's filter some records by date ranges.

Let's find business trips that will start in 10 days. We store the "From" data field in CodeMash as a date field, which converts at the end as a UNIX timestamp. See more about the Date field.

#!/bin/bash

FROM=`date -d "+10 days" +%s`

curl -X 'POST' \
  'https://api.codemash.io/v2/db/business-trips/count' \
  -H 'Authorization: Bearer ***' \
  -H 'X-CM-ProjectId: e1aa5e3b-f16d-4f43-a315-2d89f7633dc3' \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{ "filter": "{ from : { $gte : '$FROM'  }}" }'

We can pass the date range as well to find out trips from the last 30 days.

#!/bin/bash

FROM=`date -d "-30 days" +%s`
TO=`date +%s`

curl -X 'POST' \
  'https://api.codemash.io/v2/db/business-trips/count' \
  -H 'Authorization: Bearer ***' \
  -H 'X-CM-ProjectId: e1aa5e3b-f16d-4f43-a315-2d89f7633dc3' \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{ "filter": "{ from : { $gt : '$FROM' }, to: { $lt: '$TO' }}"  }'

See more examples of how you can leverage filters

And last one example. Let's find all employee business trips that are planned for the future.

We assume that:

  1. We have a country we are interested in - taxonomy term Id. E.g., We can fetch all terms from the taxonomy called countries and find the term Id of France.

  2. We have an employee Id - employee

  3. We have a date from, the Unix time

curl -X 'POST' \
  'https://api.codemash.io/v2/db/business-trips/count' \
  -H 'Authorization: Bearer ***' \
  -H 'X-CM-ProjectId: e1aa5e3b-f16d-4f43-a315-2d89f7633dc3' \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{ "filter": "{ from : { $gte : 1730969827000  }}"}'

Visit our GitHub repo to see more examples:

Try out our API:

Last updated

Was this helpful?