简体   繁体   中英

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? 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?

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

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. 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:

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.

  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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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