CodeMash
  • ๐Ÿš€Backend as a service
  • ๐Ÿฆ„Roadmap
  • ๐Ÿ•Release Notes
  • Installation
    • Managed Service
    • ๐Ÿ—๏ธAWS-CDK
    • ๐Ÿ—๏ธAzure
    • ๐Ÿ—๏ธDocker
    • ๐Ÿ—๏ธGC
    • ๐Ÿ—๏ธTerraform
  • dashboard
    • CodeMash Cloud
    • Project
    • Membership
      • Users
      • Roles
      • Policies
      • Triggers
      • Integrations
        • Apple
        • Azure
        • Facebook
        • Google
        • Twitter
      • Settings
    • Database
      • Collections
        • Schema
          • From Template
          • Structure
        • Tabs
        • Triggers
        • Indexes
        • Shared Forms
      • Taxonomies
        • Terms
        • Schema
        • Settings
      • Aggregation
      • Imports
      • Exports
      • Backups
    • Files
      • Integrations
        • CodeMash
        • S3 Bucket
    • Notifications
      • Push
        • Push Templates
        • Push Templates API
        • Push Notifications
        • Push Notifications API
        • Devices
        • Devices API
        • Integrations
          • Firebase
          • One Signal
          • Expo
      • Email
        • Email Templates
        • Emails
        • Integrations
          • AWS SES
          • Twilio Sendgrid
          • Mailgun
      • ๐Ÿ—๏ธServer events
      • ๐Ÿ—๏ธSms
    • Payments
      • Integrations
        • Apple Pay
        • Google Pay
        • Stripe
        • Kevin.
        • Paysera
        • Decta
    • Scheduler
    • Logs & Monitoring
    • Code
      • Functions
        • Function Inputs
        • Function Templates
          • Node.js
          • Python
          • Ruby
          • Java
          • Go
          • .NET Core
      • CodeMash Functions
        • Google Functions
          • Google Calendar
          • Google Gmail
        • Microsoft Functions
          • Microsoft 365 Users
          • Microsoft 365 Calendar
        • Collection Find
        • Collection Update
        • Users Find
        • Image Resize
        • Html To Pdf
        • Word Document
        • Barcode
        • QR Code
        • Send Email
        • Send Notification
        • Email Reminder
        • Notification Reminder
      • Integrations
        • AWS Lambda
        • Google Cloud Functions
        • Azure Functions
  • Other Topics
    • Apple
      • Developer Portal
      • Bundle Identifier
      • Team ID
      • Service ID
      • Key ID
    • Triggers
    • Tokens Binding
      • Project Tokens
      • Initiator Tokens
      • Receiver Tokens
      • Request Tokens
      • Operation based tokens
      • Template Functions
    • Search parameters
      • Paging
      • Filter
      • Sort
      • Projection
    • data-models
    • Errors
  • SDK
    • Node.js
    • TypeScript
    • .NET
    • ๐Ÿ—๏ธGo Lang
    • ๐Ÿ—๏ธFlutter
    • ๐Ÿ—๏ธSwift
    • ๐Ÿ—๏ธKotlin
  • CLI
    • ๐Ÿ—๏ธCodeMash CLI
  • API
    • Get Started
    • Prerequisites
    • How to test?
    • Cors
    • Project
    • Membership
      • Authentication
      • Users
    • Database
      • Collections
        • Aggregate
        • Change Responsibility
        • Count
        • Delete
        • Delete Many
        • Distinct
        • Find
        • Find One
        • Insert
        • Insert Many
        • References
        • Replace
        • Update
        • Update Many
      • Taxonomies
        • Find
    • Files
    • Code
    • Notifications
      • Push
      • Emails
      • Server Events
      • ๐Ÿ—๏ธSms
    • Payments
    • Scheduler
    • Logs & Monitoring
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. API
  2. Database
  3. Collections

Count

Get the amount of records in a collection.

PreviousChange ResponsibilityNextDelete

Last updated 2 years ago

Was this helpful?

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.

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:

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'

IRepository<BusinessTrip> Trips { get; set; }

var amount = await Trips.CountAsync();

Console.WriteLine(amount); // 15

Coming Soon

Coming Soon

Let's filter some records by date ranges.

#!/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' }}"  }'

IRepository<BusinessTrip> Trips { get; set; }

var from = DateTime.Now.AddDays(+10).ToUnixTimeMs();
var tripsIn10Days = await Trips.CountAsync(x => x.From > from);

Console.WriteLine(tripsIn10Days); // 7

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

IRepository<BusinessTrip> Trips { get; set; }

var from = DateTime.Now.AddDays(-30).ToUnixTimeMs();
var to =DateTime.Now.ToUnixTimeMs();

var recentTrips = await Trips.CountAsync(x => x.From > from && x.To <= to);

Console.WriteLine(recentTrips); // 24

The filter can be:

  1. Valid JSON (MQL - MongoDB query language)

  2. Expression<Func<T,bool>>

  3. FilterDefinition<T>

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  }}"}'

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, e.g., DateTime.Now converted to the Unix time in milliseconds

IRepository<BusinessTrip> Trips { get; set; }

var f = Builders<BusinessTrip>.Filter;

var from = f.Gte(x => x.From, from);
var inCountry = f.Eq(x => x.CountryId, country);
var withEmployee = 
      f.ElemMatch(x => x.Employees, x => x.EmployeeId == employee);

var tripsCount = 
      await Trips.CountAsync(from & inCountry & withEmployee);

So in the example above, we found the number of future trips of one particular employee to one country.

Visit our GitHub repo to see more examples:

.NET

Try out our API:

With any REST client

GET

POST

Create Employees collection and other related taxonomies from the template.

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

Please remember you need to initialize Repo first to avoid getting a NullReference exception. See how you can do that by following .

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 .

of how you can leverage filters

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 .

of how you can leverage filters in .NET

See instructions here.
See instructions here.
this link
the Date field
See more examples
the Date field
See more examples
https://github.com/codemash-io/CodeMash.Net/tree/master/tests/CodeMash.Tests/v2/Database/Collections/Count.cs
https://api.codemash.io/swagger-ui/#/db/CountRequestdbCollectionNamecount_Get
https://api.codemash.io/swagger-ui/#/db/CountRequestdbCollectionNamecount_Post
Instructions on how to test
  • GET[Count] - Get the amount of records in a collection.
  • POST[Count] - Get the amount of records in a collection.
  • Visit our GitHub repo to see more examples:
  • Try out our API:

[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
}