3

I'm trying to make a chat app that can have chatrooms with max two users. I got a collection chatrooms and chatroom-documents with an array field members that contains two user id's. Know before I can create a new chatroom I want to check if two specific users already got a chatroom together, so I need to query the two user id's with my documents.

In the end I just want to check if a chatroom exists and then return false then trying to create another chatroom with the same two specific user id's.

My problem is the query of the array content.

I tried with

chatroomsCollection.where('members', '==', firstUserID).where('members', '==', secondUserID).limit(1).get() 

and tried

chatroomsCollection.where('members','array-contains', firstUserID).where('members', 'array-contains', secondUserID).limit(1).get()

but it doesn't work.

Is it even possible to query multiple strings with elements inside an array in noSQL?

2
  • 4
    FYI there is no such language as "NoSQL". What you are asking is specific to Firestore. It's a NoSQL type database, which means there are no relational queries, such as joins. Commented Mar 16, 2019 at 20:42
  • The first attempt fails because you are comparing the array members with the user id string. The second fails because you can not have two 'array-contains' in a single query, see the section about compound queries: firebase.google.com/docs/firestore/query-data/queries Commented Mar 16, 2019 at 20:49

1 Answer 1

7

If you want to query like this, you will need a different data structure. Consider instead an object type field like this:

members: {
    'uid1': true,
    'uid2': true
}

Then you can query documents like this:

chatroomsCollection
    .where('members.uid1', '==', true)
    .where('members.uid2', '==', true)
    .limit(1)
    .get()
Sign up to request clarification or add additional context in comments.

5 Comments

I agree, this is probably the way to go.
I already saw this structure in an example, but was not sure with two things whether I could implement it with this structure. 1. Can I still use array-contains if I want to query all the chatrooms a user is member of? 2. Do I have to perform two individual queries if the order of the strings passed in the request is the other way round?
@ConstantinBeer 1. No, not for objects. 2. No.
@DougStevenson You helped me a lot and saved me a lot of time. Thank you!
I am currently using this kind of structure but with attributes named "user1" and "user2", I need to have a query to find if there is a document which have two specific strings under "user1" and "user2". The problem is that I don't know in advance the order. So an example: I have two input strings: "Apple" and "Banana", and I want to check if this document exists - there are two options: "user1" = "Apple", "user2" = "Banana" OR "user1" = "Banana", "user2" = "Apple". Both of these situations are valid and I want to retrieve it. How would I write this query?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.