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.
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
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".
The CodeMash API version used to fetch data from the API. If not specified, the last version will be used. E.g.: v2
Filter cannot be passed as query string, use POST method instead.
A limit on the number of objects to be returned, between 1 and 100.
The number of records to skip before counting
Accept Header
API key of your cluster. Can be passed in a header as X-CM-Cluster.
ID of your project. Can be passed in a header as X-CM-ProjectId.
Specify culture code when your response from the API should be localised. E.g.: en
GET //{version}/db/{CollectionName}/count HTTP/1.1
Host: api.codemash.io
Authorization: YOUR_API_KEY
X-CM-ProjectId: text
Accept: */*
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
}
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
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".
The CodeMash API version used to fetch data from the API. If not specified, the last version will be used. E.g.: v2
Accept Header
API key of your cluster. Can be passed in a header as X-CM-Cluster.
ID of your project. Can be passed in a header as X-CM-ProjectId.
Specify culture code when your response from the API should be localised. E.g.: en
A query (in JSON format) that specifies which records to count
A limit on the number of objects to be returned, between 1 and 100.
The number of records to skip before counting
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"
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.
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' }}" }'
And last one example. Let's find all employee business trips that are planned for the future.
We assume that:
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.
We have an employee Id - employee
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?