简体   繁体   English

Unity - 将相同字段的多个数据存储到 Firestore 中的相同文档 ID

[英]Unity - Storing Multiple Data of Same Fields to Same Document ID in Firestore

How do you add data to Firestore without over-writing previous data for the same key?如何将数据添加到 Firestore 而不会覆盖相同密钥的先前数据? For example, I want to add a document called "Player 1" to the Collections "Scores."例如,我想在 Collections “Scores”中添加一个名为“Player 1”的文档。 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.我想跟踪玩家 1 的所有分数,无论同一玩家玩了多少次游戏。 In other words, how do you append data to a Firestore document?换句话说,你如何将 append 数据放到一个 Firestore 文件中?

This is the code I have for writing the data to Firestore.这是我将数据写入 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");
}

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:如果您使用相同的 documentID,您可以使用 2 种方法来实现这一点,并且它们都需要对您的 Firestore 结构进行一些重组:

  1. Use an array of scores:使用分数数组:

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:为此,您必须具有以下 Firestore 结构:

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

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.这种方法的问题在于,随着游戏的进行,您将不得不在该数组中保存许多记录,并且每次阅读此文档时都需要获取所有记录,这可能会导致游戏性能不佳,并且还有大小限制每个文档 1MB 的文档,这可能会很快被破坏,所以我不推荐这种方法,但它仍然是一种选择。

  1. Have a score subcollection:有一个分数子集合:

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:为此,您必须具有以下 Firestore 结构:

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.使用这种方法,您可以更灵活地一次获取多少分数,从而获得更好的性能,此外,您可以在集合中存储多少文档没有限制,因此您不必担心文档的大小.

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.注意:我没有共享任何代码,因为我认为这更像是 Firestore 问题的结构而不是编码问题,但是如果您在实现执行此任务的代码时遇到任何问题,请告诉我,我可以帮助您。

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

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