简体   繁体   中英

How do I get the position of a document in Firebase Cloud Firestore using swift?

I created a database in CloudFirestore, now I want to retrieve the position number of the documents from it (ie: jolly's position should be "2"), I tried to do it using the.whereField but I was unable to read the querySnapshot.count outside the query.

我的数据库快照在这里!

I am trying to do it like this:

db.collection("TimeSlots")
         .whereField("written", isEqualTo: true)
         .getDocuments()  { (querySnapshot, err) in
         if err != nil {

                print("Error getting documents: (err)")
          } else {

                 position = querySnapshot?.count as? Int                                  
             }
         }

so here as soon as an entry is added i am getting its position in the "position" variable but I am unable to use the position value outside this method.

Please and thank you!

Firestore documents don't have a "position". The order of documents you see in the console is always going to be alphabetical (actually lexicographically ) by document ID. There are no queries that let you specify any index into a collection, as those types of operations do not scale massively as Firestore requires.

Here is how I managed to index position of documents from the collection (Using question's cloud firestore as an example):-

 db.orderBy("time", Query.Direction.DESCENDING)
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>()
        {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task)
            {
                if(task.isSuccessful())
                {
                    if(Objects.requireNonNull(task.getResult()).size() > 0)
                    {
                        int i = 0; //temporary index
                        Map<Integer, Object> _docList = new HashMap<>(); // A hashMap to store document id

                        for(DocumentSnapshot _documentSnapshot : Objects.requireNonNull(task.getResult()))
                        {
                            _docList.put(i, _documentSnapshot.getId());//Store temporary index (i) mapping to each document

                            //Checks if i equals to the documentIndex needed.
                            //Let's say documentIndex is 2 (Declared as a parameter of a method).
                            if(i == documentIndex)
                            {
                                //Do something with the document at that index.
                                _collectionReference.document(_documentSnapshot.getId()).delete(); //Here I am deleting the jolly document.
                                return;
                            }

                            i++; //Increase the temp index if the statement is not true
                        }
                    }
                }
            }
        });

I have commented the code for easy understandability. Hope this will help someone out there. Though I used Java hope the Swift guys will understand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM