问题
I working in a firebase database. I need to limit the length of a string field. How do I do that?
The path to the field is:
Col1/doc1///description
That is, starting with the collection col1, then into doc1, then for all collections under doc1, and all documents under that collection, the description field needs to be limited to 100 characters.
Can someone please explain to me how to do this? Thanks
回答1:
For Cloud Firestore you can validate that the description field is no longer than 100 characters with:
service cloud.firestore {
match /databases/{database}/documents {
match /col1/doc1 {
allow write: if resource.data.description.length <= 100;
match /subcollection1/{doc=**} {
allow write: if resource.data.description.length <= 100;
}
}
}
}
This applies to col1/doc and all documents in subcollection1. Note that these rules will not limit the length of the description, since security rules cannot modify the data that is written. Instead the rules reject writes where the description is longer than 100 characters.
There is no way (that I know of) to apply rules to each subcollection of only one document. The closest I know is to apply it to all documents and their subcollections:
match /col1/(document=**} {
allow write: if resource.data.description.length <= 100;
}
This applies the validation to all documents in col1 and in all subcollections under that.
来源:https://stackoverflow.com/questions/53437482/how-to-limit-string-length-in-firebase