简体   繁体   中英

How to update a specific(nth) child of a key in firebase

I know that Firebase has some very basic query functionality with orderBy , limitTo , startAt , endAt ...., etc. For updating has methods like update , set . In my case ai have an structure like this:

在此处输入图片说明

So, there is a list of keys on my PIN , how can I update the child pin of a specific key with javascript?

For example, I want now to update the pin of the second key. Now it has 20 , and I want to set it to 30 . But please, keep in mind that the list most have more than 2 keys, so maybe later I want to update the child pin of the fourth generated key.

How can I do this?

Relative ordering like "second" or "fourth" are pretty useless here. If you want to update the pin of the node with key -KrnO...Ho you can do so with:

firebase.database().ref("PIN/`-KrnO...Ho/PIN").set(30);

If you want to update the PIN of the user named cindy, you first need to query to determine her key:

firebase.database().ref("PIN").orderByChild("name").equalTo("cindy").once("value", function(snapshot) {
  snapshot.forEach(function(user) {
    user.ref.child("PIN").set(30);
  });
})

The loop in that second snippet is needed, since there can technically be multiple users named cindy.

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