logo
down
shadow

How to delete all but most recent X children in a Firebase node?


How to delete all but most recent X children in a Firebase node?

By : Mike
Date : December 01 2020, 05:00 PM
it should still fix some issue Keep the most recent N items, is one of the trickier use-cases to implement. If you have any option to change it into "keep items from the past N hours", I recommend going that route.
The reason the use-case is tricky, is that you're counting items and Firebase does (intentionally) not have any count-based operations. Because of this, you will need to retrieve the first N items to know which item is N+1.
code :
ref.child('lines').once('value', function(snapshot) {
  if (snapshot.numChildren() > MAX_COUNT) {
    var childCount = 0;
    var updates = {};
    snapshot.forEach(function (child) {
      if (++childCount < snapshot.numChildren() - MAX_COUNT) {
        updates[child.key()] = null;
      }
    });
    ref.child('lines').update(updates);
  }
});
lineids
  --K3qx02jslkdjNskjwLDK
  --K3qx23jakjdz9Nlskjja
  --K3qxRdXhUFmEJdifOdaj
ref.child('lineids').once('value', function(snapshot) {
  if (snapshot.numChildren() > MAX_COUNT) {
    var childCount = 0;
    var updates = {};
    snapshot.forEach(function (child) {
      if (++childCount < snapshot.numChildren() - MAX_COUNT) {
        updates['lineids/'+child.key()] = null;
        updates['lines/'+child.key()] = null;
      }
    });
    ref.update(updates);
  }
});


Share : facebook icon twitter icon
In Firebase, is there a way to get the number of children of a node without loading all the node data?

In Firebase, is there a way to get the number of children of a node without loading all the node data?


By : JesusCupcakes
Date : March 29 2020, 07:55 AM
like below fixes the issue The code snippet you gave does indeed load the entire set of data and then counts it client-side, which can be very slow for large amounts of data.
Firebase doesn't currently have a way to count children without loading data, but we do plan to add it.
code :
var upvotesRef = new Firebase('https://docs-examples.firebaseio.com/android/saving-data/fireblog/posts/-JRHTHaIs-jNPLXOQivY/upvotes');
upvotesRef.transaction(function (current_value) {
  return (current_value || 0) + 1;
});
{
  "posts" : {
    "-JRHTHaIs-jNPLXOQivY" : {
      "upvotes_count":5,
      "upvotes" : {
      "userX" : true,
      "userY" : true,
      "userZ" : true,
      ...
    }
    }
  }
}
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.countlikes = functions.database.ref('/posts/$postid/upvotes').onWrite(event => {
  return event.data.ref.parent.child('upvotes_count').set(event.data.numChildren());
});
exports.countPrescriptions = functions.database.ref(`/prescriptions`).onWrite((change, context) => {
    const data = change.after.val();
    const count = Object.keys(data).length;
    return change.after.ref.child('_count').set(count);
});
Copying Firebase node with multiple children to another node

Copying Firebase node with multiple children to another node


By : Megha Gajbhiye
Date : March 29 2020, 07:55 AM
seems to work fine Hey I would like to copy the data inside the child node with multiple sub-nodes to another place. , Though not a very good approach but this approach get the work done.
code :
public void copyFirebaseData() {
    DatabaseReference questionNodes = FirebaseDatabase.getInstance().getReference().child("questions");
    final DatabaseReference toUsersQuestions = FirebaseDatabase.getInstance().getReference().child("users").child(uid).child("questions");

    questionNodes.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot questionCode : dataSnapshot.getChildren()) {
                String questionCodeKey = questionCode.getKey();
                String correctAnswerString = questionCode.child("correctAnswer").getValue(String.class);
                String imageUrlString = questionCode.child("imageUrl").getValue(String.class);
                toUsersQuestions.child(questionCodeKey).child("imageUrl").setValue(imageUrlString);
                toUsersQuestions.child(questionCodeKey).child("correctAnswer").setValue(correctAnswerString);

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}
How to retrieve array data from children node in Firebase Realtime Database Java? (Firebase Recycler Adapter)

How to retrieve array data from children node in Firebase Realtime Database Java? (Firebase Recycler Adapter)


By : user3007212
Date : March 29 2020, 07:55 AM
it helps some times The problem in your code is that your url_photo_collage field is declared in your DolanItemClass class of type String while in your database is an array. To solve this, change the type of your field from String to a List and get it accordingly in your adapter.
Firebase - How to update many children and not delete others in AngularFire

Firebase - How to update many children and not delete others in AngularFire


By : Hardik Khalasi
Date : March 29 2020, 07:55 AM
I wish this help you Update is not a recursive operation. So it's calling set on the child paths. If you call update at users/, you're saying don't delete any keys under user/ which are not in my data, when you want to say, don't delete any keys in the child records of user/.
Instead, iterate the records and call update on each:
How to delete an object with multiple children in Firebase?

How to delete an object with multiple children in Firebase?


By : Phil Bokeh
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I am trying to delete an object with multiple children in my Firebase database. My data is setup like so: , Sometimes simpler is better:
shadow
Privacy Policy - Terms - Contact Us © animezone.co