Auto increment a value in firebase with javascript

后端 未结 5 2081
野性不改
野性不改 2020-12-03 14:52

Hello I\'m working on some firebase project and i can save my datas via javascript into firebase database. But i couldn\'t figure it out to auto increment child value (my ch

相关标签:
5条回答
  • 2020-12-03 15:11

    Firebase recommends using distributed counters: https://firebase.google.com/docs/firestore/solutions/counters

    For the document that requires the counter, you'd initialize a subcollection of, say, 10 documents and run transaction increments on a random shard whenever you need a counter incremented.

    Then you'd query the collection of shards and sum their counters.

    0 讨论(0)
  • 2020-12-03 15:15

    Firebase has no auto-incrementing keys, since those don't work well in massively multi-user systems where clients can be offline for prolonged periods.

    Instead, Firebase has its own type of auto-generated keys called push IDs. These push IDs have the same important properties of sequences in many relational databases: they are ordered and sequential. But in addition, they can be calculated client-side, even when the client is not connected to the Firebase servers.

    See the Firebase documentation on saving data in lists, this legacy blog post on why arrays don't work well in Firebase and this post on how push ids work.

    0 讨论(0)
  • 2020-12-03 15:21

    You can use for first column key to "USERID + Your_indexNumber(in client var)" etc.

    This is very simple method.

    Your table must like this:

    https://i.stack.imgur.com/k9UI6.png

    0 讨论(0)
  • 2020-12-03 15:23

    IMHO, I think we should use transaction to save increment_id. Please take a look here: https://firebase.google.com/docs/database/web/read-and-write#save_data_as_transactions

    function toggleStar(postRef, uid) {
      postRef.transaction(function(post) {
        if (post) {
          if (post.stars && post.stars[uid]) {
            post.starCount--;
            post.stars[uid] = null;
          } else {
            post.starCount++;
            if (!post.stars) {
              post.stars = {};
            }
            post.stars[uid] = true;
          }
        }
        return post;
      });
    }
    
    0 讨论(0)
  • 2020-12-03 15:35

    There is no AutoId in firebase but here is a quick way to set as auto id

    Forexample ;

    1- create yourModel ( which one u gonna send as model to firebase )

    2- DatabaseReference firebaseDatabase;

    3- firebaseDatabase =FirebaseDatabase.instance.reference();

    4- firebaseDatabase.child("table_name").push().set( yourModel.toJson() );

    Also for getting data u can write code like that

    var result= firebaseDatabase.child("table_name").once().then(
       (DataSnapshot datasnapshot){
         Map<dynamic,dynamic> values= datasnapshot.value;
         values.forEach((key,value){
           print("key:"+key+" value:"+value["name"]);
         });
       }
     );
     print(result);
    

    I tried it and works great

    Have a nice day !!!

    0 讨论(0)
提交回复
热议问题