DynamoDB — cannot specify projection when Select is COUNT
TL;DR — Your Query/Scan set Select: COUNT and a ProjectionExpression (or the legacy AttributesToGet). COUNT returns only the number of matching items, never the items themselves, so naming attributes to return is contradictory — DynamoDB rejects it. Pick one: Select: COUNT with no projection, or a projection with Select: SPECIFIC_ATTRIBUTES (the default when you supply a ProjectionExpression).
What it means
ValidationException: Cannot specify the AttributesToGet when choosing to get only the Count
ValidationException: Cannot use ProjectionExpression when Select is COUNTSelect controls what a read returns. COUNT asks DynamoDB for a tally (Count/ScannedCount) and no item data. A ProjectionExpression/AttributesToGet asks for specific attributes of returned items. Those two are mutually exclusive — you can't count items and also project their attributes in the same call.
Why it happens
- Both parameters set at once —
Select: COUNTleft in place while aProjectionExpressionwas added (or vice versa). - Legacy
AttributesToGetcombined withSelect: COUNT. - A query builder that always attaches a projection, even when the caller only wanted a count.
- Misunderstanding
Select— expectingCOUNTto also hand back the items it counted.
How to fix it
- Just counting? Drop the projection. Set
Select: COUNTand removeProjectionExpression/AttributesToGet. - Need specific attributes? Drop
Select: COUNT. Supply aProjectionExpression; DynamoDB defaultsSelecttoSPECIFIC_ATTRIBUTES, so you usually don't setSelectat all. - Need both the count and the items? Make one call for the items (with your projection) and read its
Count, or make a separateSelect: COUNTcall. - Prefer
ProjectionExpressionoverAttributesToGet— the legacy parameter is discouraged and only works withSelect: SPECIFIC_ATTRIBUTES.
Just want a quick count or a projected view while exploring? The DynoTable desktop app runs queries and shows the matched count without you hand-assembling conflicting Select parameters.
Related errors
- Cannot use both expression and non-expression parameters — mixing legacy and expression params.
- Query key condition not supported — a malformed key condition on the same request.
- Learn: Count, sum & aggregate · Projection expressions