Unity-将相同字段的多个数据存储到Firestore中的相同文档ID [英] Unity - Storing Multiple Data of Same Fields to Same Document ID in Firestore

查看:37
本文介绍了Unity-将相同字段的多个数据存储到Firestore中的相同文档ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不覆盖同一密钥的先前数据的情况下将数据添加到Firestore?例如,我想添加一个名为"Player 1"的文档.收集得分".我想跟踪玩家1的所有得分,无论同一位玩家玩游戏的时间有多长.换句话说,如何将数据附加到Firestore文档?

How do you add data to Firestore without over-writing previous data for the same key? For example, I want to add a document called "Player 1" to the Collections "Scores." I want to keep track of all the scores for Player 1, no matter how may times the game is played by the same player. In other words, how do you append data to a Firestore document?

这是我用于将数据写入Firestore的代码.我如何确保数据"每次为同一用户运行此代码时,代码不会被覆盖?对于如何实现这一点的任何帮助,将不胜感激:

This is the code I have for writing the data to Firestore. How do I make sure that the "data" is not over-written every time this code runs for the same user? Any help in how to implement this would be appreciated:

public void LogGameStats()
{

    var data = new Dictionary<string, object>{
      {"Number of Targets Collected", gameStats.targetCollectedCount},
      {"Number of Walls Hit", gameStats.wallHitCount},
      {"Number of Hazards Hit", gameStats.hazardHitCount},

      };

      StartCoroutine(WriteDoc(GetDocumentReference(), data));
      Debug.Log("I logged something");
}

推荐答案

如果您使用相同的documentID,则可以使用2种方法来实现,而这两种方法都需要对Firestore结构进行一些重构:

If you are using the same documentID you could use 2 approaches to achieve that and both of them would require some re-structuring of your Firestore Structure:

  1. 使用分数数组:

使用这种方法,您将在每次用户获得新分数时将记录添加到分数数组,而最新分数将是该数组中的最后一条记录.为此,您必须具有以下Firestore结构:

With this approach you would add records to the score array everytime the user gets a new score and the latest score would be the last record in the array. For that you would have to have the following Firestore structure:

Player Collection
    playerId,
    [
        {
            targetCollectedCount,
            wallHitCount,
            hazardHitCount
        },
        {...}
    ]

这种方法的问题是,随着游戏的进行,您将不得不在该数组中记录许多记录,并且每次阅读此文档时都将获取所有记录,这可能会导致游戏性能下降,而且每个文档的文件大小限制为1MB,使用此方法可能会很快被突破,因此我不建议您使用这种方法,但这仍然是一种选择.

The problem with this approach is that as the game progresses you would have to many records in that array and you would to fetch all the records everytime you read this document, this could generate a poor performance in your game, also there is a size limit of documents of 1MB per document, which could be breached fast with this, so I would not recommend this approach, but it is still an option.

  1. 具有得分子集合:

使用这种方法,您将在用户每次获得新分数时创建一个新的分数文档,还可以添加一个时间戳以使其更容易获得最新分数.为此,您必须具有以下Firestore结构:

With this approach you would create a new score document everytime the user gets a new score, you can also add a timestamp to make it easier to get the latest score. For that you would have to have the following Firestore structure:

Player Collection
    playerId,
    Score Subcollection
        scoreId,
        targetCollectedCount,
        wallHitCount,
        hazardHitCount,
        scoreTimestamp

通过这种方法,您可以一次获取多少个乐谱以提高性能,从而具有更大的灵活性,而且可以存储在集合中的文档数量没有限制,因此您不必担心文件大小.

With this approach you have more flexibility in how many scores you want to fetch at a time which leads to better performance, plus there is no limit in how many documents you can store in a collection so your won't have to worry size of documents.

注意:我没有共享任何代码,因为我认为这更多的是Firestore问题的结构而不是编码的结构,但是如果您在执行此任务的代码方面遇到任何问题,请告诉我我可以为您提供帮助.

NOTE: I have not shared any code as I think this is more of a structure of Firestore issue than a coding one, but if you have any problems implementing code that does this tasks let me know and I can help you with that.

这篇关于Unity-将相同字段的多个数据存储到Firestore中的相同文档ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆