-1

This is the rule which I use to read/write the data into the Firestore:how can I prevent the msg data to be duplicated from the user?

rules_version = '2';

service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true; }
} 

}
Catalina
  • 663
  • 5
  • 20
  • 1
    There is no way to prevent duplicate **values** in security rules, as it would require the rules to consider all your documents, which doesn't scale. See https://stackoverflow.com/questions/58960349/cloud-firestore-rules-proper-syntax/58960967#58960967 and some more of these: https://stackoverflow.com/search?q=%5bgoogle-cloud-firestore%5d%5bfirebase-security%5d%20duplicates. If something must be unique, you should use it as the key/ID of your documents, so that your security rules can check for the *existence* of a specific document that that key/id. – Frank van Puffelen Dec 16 '19 at 01:26

1 Answers1

0

Natively, in Cloud Firestore you can't ensure uniqueness of data from neither Rules or the SDK. Although I believe that the following links will help you to achieve this, as they apply to similar use cases:

The accepted answer(credits to Frank van Puffelen) in this Stackoverflow Post explains how to achieve what you want with a workaround.

Check also this article in which the approach is quite simple.

Generally in order to check uniqueness you should make a collection "X" in which every document corresponds to an "x" value that you need to be unique. The documentID of each document in the collection should named after the value you want to be unique. Thus when you try to save your search results to Firestore, check the "X" collection to see if there is a documentId that fits the search results you would like to add every time. If the documentId doesn't exist then you add the search results to your collection. The misconvience in your use case is that the documentID of the document should contain the message as a name in order to work.

Let me know if this was helpful.

tzovourn
  • 1,293
  • 8
  • 18