Add new field or change the structure on all Firestore documents

前端 未结 5 712
感动是毒
感动是毒 2021-02-02 13:11

Consider a collection of users. Each document in the collection has name and email as fields.

{
  \"users\": {
    \"uid1\         


        
5条回答
  •  庸人自扰
    2021-02-02 13:16

    To solve this, you need to update each user to have the new property and for that I recommend you to use a Map. If you are using a model class when you are creating the users as explained in my answer from this post, to update all users, just iterate over the users collection amd use the following code:

    Map map = new HashMap<>();
    map.put("timestamp", FieldValue.serverTimestamp());
    userDocumentReference.set(map, SetOptions.merge());
    

提交回复
热议问题