1

I have documents in Firestore like the following:

BID: "123"
From: "xxx"
Opn: true

I need to check if there are another document have BID == "123" and Opn == true before create the document, because it's not possible to have one more document where Opn is true with same BID.

I try to use get() and exists but it's not working with 2 data fields.

What I tried:

function checkIfThereOpenRoomForSameBBB(xxx) { 
  return !(
    (get(/databases/$(database)/ChatRooms/$(ChatRoom)).data.BID == xxx) &&
    (get(/databases/$(database)/ChatRooms/$(ChatRoom)).data.Opn == true)
  ); 
}

is there any solution for this case?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Amjad
  • 25
  • 1
  • 5
  • I Used something like that: – Amjad May 06 '20 at 23:32
  • function checkIfThereOpenRoomForSameBBB(xxx) { return !((get(/databases/$(database)/ChatRooms/$(ChatRoom)).data.BID == xxx) && (get(/databases/$(database)/ChatRooms/$(ChatRoom)).data.Opn == true)); } – Amjad May 06 '20 at 23:35

1 Answers1

0

Security rules can't search for data in the database, as that would be prohibitively slow and expensive. All they can do is check if a document exists at a specific path, or read a document as a specific path and check its contents.

This means that any time you want to check if something exists, you'll need to ensure that lives at a known path. So if the combination of BID and Opn=tru must be unique, you should create a collection where the key of each document consists of the BID value and Opn=true.

If this is a global requirement for your app, you could even use this key in your existing collection instead of the (likely auto-generated) key you currently use.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807