set.count()

Get the number of values in a Set.

Signature

count() => Int

Description

The count() method gets the number of values in the Set.

Because this method scans the full Set, it returns an error if there are more than 16,000 documents in the Set. This method can timeout for large Sets under that limit.

A document update stores a new version of the document for which counter data is poorly suited for database storage.

If a frequently updated counter is essential, an event-sourcing technique is recommended to reduce database contention and reduce unnecessary database operations.

If the event sourcing pattern isn’t suitable, the following improvements might be considered:

  • Set a collection history_days field to a small value, with a zero value recommended. Document history continues to be collected, but is removed sooner than the default zero days.

  • Periodically, run a query to explicitly remove document history.

  • Instead of attempting to implement a real-time counter, consider storing countable documents as a cache and periodically analyzing cache contents to update a reporting document.

Parameters

None

Return value

Type Description

Int

Count of values in the Set.

Examples

Get a count Product documents with a name of limes:

Product.all().count()
9

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!