set.count()

Get the number of elements in a Set.

Signature

count() => Number

Description

Gets the number of elements in the calling Set.

Eager loading

This method uses eager loading and requires a read of each document in the calling Set. For large Sets, this may result in poor performance and high costs.

Avoid use on large sets

This method scans the full Set, which can cause many reads and might time out for large Sets.

Considerations

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 the collection’s history_days setting 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

Number

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!