0

So currently building an app that connects users with stores. When a user presses connect, he connects with the first store available in the queue. Once a connection is established, the user can chat with that store about potential deals.

Currently, when a user presses connect, I am ordering the queue of stores in stores collection by timestamp and I am fetching the first document and then proceed to establish the connection.

This is incorrect because I must not connect the user with a store he has already connected with before. How do I exclude an array of already connected document IDs while doing the order by and limit to operations? Wherefield is only useful if I want to filter by fields. In this case, I want to exclude certain documents while fetching documents.

fileprivate func checkIfConnectionExists(){

        Firestore.firestore().collection("stores").order(by: "timestamp").limit(to: 1).getDocuments { (querySnapshot, error) in

            if let error = error{

                return

            } else {

                if let docs = querySnapshot?.documents, !docs.isEmpty{

                    for document in docs{

                        //HERE I WRITE THE CODE THAT ESTABLISHES THE CONNECTION BETWEEN THE STORE AND THE USER
                    }
                } else {

                    self.connectionUnavailable()
                }
            }
        }
}
Archid
  • 377
  • 6
  • 21
  • 1
    Firestore does not offer exclusions in queries. You can only use the values of properties that actually exist in order to filter documents. Your best bet is to always have field populated with some value that identifies its status for the purpose of queries. – Doug Stevenson Jan 13 '20 at 01:19
  • @DougStevenson thanks for the info. I guess I should rethink the architecture. Do you have any suggestions on how I can go about this? I can create a field for each store that contains an array of all historical connected users and then check that array but I'm still not sure if this can be done that way – Archid Jan 13 '20 at 04:21
  • Is it possible to write something like - Firestore.firestore().collection("stores").whereField("historicalConnections" contains currentUserID) - I know the syntax is wrong but is it possible to do something like this with wherefield? – Archid Jan 13 '20 at 04:23
  • 1
    https://firebase.google.com/docs/firestore/query-data/queries#array_membership – Doug Stevenson Jan 13 '20 at 04:24

0 Answers0