Home > OS >  How to update a field in document in a firestore
How to update a field in document in a firestore

Time:05-29

Im trying to update a string field in specific document using Firebase Firestore for my android app but every method I see is while knowing the document refernce which I find difficult to find in my program. Would like for some help for another method or help in finding the document refernce using a specific field value. Thanks in advance. (using C# btw)

private async Task<string> GetDocRefAsync(string userId)
        {
            Object obj = await FirestoreData.GetFirestore().Collection(DBConstants.FS_COLLECTION_USERS).
                    WhereEqualTo(DBConstants.FS_COLLECTION_USERS_USER_ID, userId).Get();
            QuerySnapshot snapshot = (QuerySnapshot)obj;
            if (snapshot.IsEmpty)
            {
                Log.Debug("UpdateGroupAsync", "userId: "   userId   " not found");
                return null;
            }
            string docRef = "";
            foreach (DocumentSnapshot item in snapshot.Documents)
            {
                //docRef = item.;
                
            }
            return docRef;
        }

Firstly ive tried to find the document ref using this code but dont have a function to get the ref even after getting the correct document. the fourth line from the bottom is where I couldnt find it.

database pic

this.groupCode = code;
                string strUserRef = GetDocRefAsync(userRef).ToString();
                DocumentReference docReference = database.Collection(DBConstants.FS_COLLECTION_USERS_GROUPS_CODE).Document(strUserRef);
                docReference.Update(DBConstants.FS_COLLECTION_USERS_GROUPS_CODE, groupCode);

CodePudding user response:

If you want to get the documents where a field has a given value, you can use a query. Then once the query returns, you can get documents IDs with the .Id field on each DocumentShapshot in the returned documents.

You will also need to add await for the returned value since it is an async method returning a Task<string> not returning a string.

private async Task<string> GetDocRefAsync(string userId) {
    CollectionReference usersRef = FirestoreData.GetFirestore().Collection(DBConstants.FS_COLLECTION_USERS);
    Query query = usersRef.WhereEqualTo(DBConstants.FS_COLLECTION_USERS_USER_ID, userId);

    // or GetSnapshotAsync depending on the version of firebase
    QuerySnapshot querySnapshot = await query.Get();
       
    // Note: if it matches multiple documents this will just return
    // the ID of the first match
    foreach (DocumentSnapshot item in snapshot.Documents)
    {
        return item.Id;
    }

    Log.Debug("UpdateGroupAsync", "userId: "   userId   " not found");
    return null;
}

And you can use it like this to update a document (note that you were using a different collection here - probably by mistake).

string userDocId = await GetDocRefAsync(userId);

CollectionReference userCollection = database.Collection(DBConstants.FS_COLLECTION_USERS);
DocumentReference docReference = userCollection.Document(userDocId);

// or UpdateAsync depending on your version of firebase
docReference.Update(DBConstants.FS_COLLECTION_USERS_GROUPS_CODE, groupCode);
  • Related