skip to Main Content

So I’m trying to search Firestore Database to find all Fields ExeName
like it is underscored on this screen:

FireStore database

and find arrays setnumber with the same key as underscored ExeName Field

This is my DataModel

I was searching a lot of time and can’t find anything about that

2

Answers


  1. Chosen as BEST ANSWER

    So I found idea to fetch all data to my project and then search for the specified string and its key to find corresponding field with the same key value using this function:

    func searchAndUpdate(searchString: String) {
           let sevenDaysInSeconds: TimeInterval = 7 * 24 * 60 * 60
           let currentDate = Date()
           let sevenDaysAgo = currentDate.addingTimeInterval(-sevenDaysInSeconds)
           
           for (index, data) in info.enumerated() {
               for (key, value) in data.ExeName {
                   if value == searchString {
                      
                       if let weightArray = data.weight[key], data.time > sevenDaysAgo.timeIntervalSince1970 {
                           let summary = Summary(Name: data.ExeName[key] ?? "",
                                                 weig: weightArray,
                                                 interval: data.time)
                           chartData.append(summary)
                           
                        
                        break
                       }
                   }
               }
           }
       }
    

  2. The below is JavaScript(ish), but should be nearly identical in Swift:

    const valueToSearchFor = "Dumbbell Seated Scaption";
    
    const myCollRef = collection(db, "some_collection");
    
    const q = query(myCollRef,
                    where("ExeName", "array-contains", valueToSearchFor),
                    limit(1));
    
    const querySnap = await getDocs(q);
    const docSnap = querySnap.docs[0]; // pull out the first result
    const theData = docSnap.data();
    
    // find the position of `valueToSearchFor` in the "ExeName" field:
    const position = theData.ExeName.indexOf(valueToSearchFor);
    
    // get the "numberreps" array entry for corresponding `valueToSearchFor`
    const numberRepsArray = theData.numberreps[position];
    
    console.log("number of reps:", numberRepsArray);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search