简体   繁体   English

如何更新firestore中文档中的字段

[英]How to update a field in document in a firestore

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.我尝试使用 Firebase Firestore 为我的 android 应用程序更新特定文档中的字符串字段,但我看到的每个方法都是在知道我在程序中很难找到的文档引用的同时。 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) (使用 C# 顺便说一句)

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);

If you want to get the documents where a field has a given value, you can use aquery .如果要获取字段具有给定值的文档,可以使用查询 Then once the query returns, you can get documents IDs with the .Id field on each DocumentShapshot in the returned documents.然后,一旦查询返回,您就可以在返回的文档中的每个DocumentShapshot上使用.Id字段获取文档 ID。

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 .您还需要为返回的值添加await ,因为它是一个返回Task<string>而不返回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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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