dkumar
dkumar

Reputation: 338

firestore document snapshot get() does not work with dotted string notation of field-names containing hyphen

I've a simple document called 'agents' under a collection named 'mycoll' with data set something like below:

{
 'metadata': {
   'agent-ids': ['fdfd', 'asdfasdf', 'rerere'],
   'agent_ids': ['foo1', 'booo']
}

I got the document snapshot:

snapshot = firestore.client().document('mycoll/agents').get()

If I try to access 'agent-ids' field name using get() method on this snapshot:

agent-list-with-hypens = snapshot.get('metadata.agent-ids')

ValueError: Path metadata.agent-ids not consumed, residue: -ids

However, If I try to access 'agent_ids' using get() method, that works just fine:

print(snapshot.get('metadata.agent_ids'))

['foo1', 'booo']

My question is what is causing this different behavior for field-names with an '-' and why? Any documentation which explains about this? I understand that snapshot.get() accpets a FieldPath argument instead of plain string but existing API documentation does not warn that field-names with an '-' are not allowed in field-path name strings delimited by '.' In fact, snapshot.get(firestore.client().field_path('metadata', 'agent-ids')) works just fine.

Upvotes: 1

Views: 1052

Answers (1)

Yanan C
Yanan C

Reputation: 301

Based on the documentation, here are the constraints on field paths:

  • Must separate field names with a single period (.)
  • Must enclose each field name in backticks unless the field name meets the following requirements:
    • The field name contains only the characters a-z, A-Z, 0-9, and underscore (_)
    • The field name does not start with 0-9

So a field name/path containing dash will raise ValueError. The above constraints also explain why snapshot.get(firestore.client().field_path('metadata', 'agent-ids')) works just fine is because the field name is enclosed in backticks.

Upvotes: 1

Related Questions