MQLN
MQLN

Reputation: 2328

Getting Document Multiple Times in Firestore Rules

When writing rules for a Firebase Database that require fetching a document from a separate collection, is there a difference in referring to that document more than once?

For example, this will cause a single query of the otherStuff collection:

// Rules single get
match /someData/{dataId} {
   allow read: if get(/databases/$(database)/documents/otherStuff/$(dataId)).data.allowRead == true
}

But will the following code actively get the same document twice? Or is it optimized, and only fetched once?

// Rules twice get
match /someData/{dataId} {
   allow read: if get(/databases/$(database)/documents/otherStuff/$(dataId)).data.allowRead == true
&& get(/databases/$(database)/documents/otherStuff/$(dataId)).data.reallyAllowRead == true
}

Upvotes: 3

Views: 911

Answers (1)

Juan Lara
Juan Lara

Reputation: 6854

Check out this section in the docs, https://firebase.google.com/docs/firestore/security/rules-structure#security_rule_limits

In security rule evaluation for a single rule, "multiple requests for the same document do not count as separate requests."

Upvotes: 4

Related Questions