You're viewing the legacy docs. They are deprecated as of May 18, 2016.
These docs are for version 2.4.2 and below of the Javascript SDK. Go to our current docs, or see our Web migration guide.

Version 2.4.2Changelog


The limitToFirst() method is used to set a maximum number of children to be synced for a given callback. If we set a limit of 100, we will initially only receive up to 100 child_added events. If we have less than 100 messages stored in our database, a child_added event will fire for each message. However, if we have over 100 messages, we will only receive a child_added event for the first 100 ordered messages. As items change, we will receive child_removed events for each item that drops out of the active list, so that the total number stays at 100.

Using our sample Firebase of dinosaur facts, we can find the two shortest dinosaurs with this query:

var ref = new Firebase("");
ref.orderByChild("height").limitToFirst(2).on("child_added", function(snapshot) {

Our child_added callback will be triggered exactly two times, unless there are less than two dinosaurs store in the database. It will also get fired again if one of the first two dinosaurs is removed from the data set, as a new dinosaur will now be the second shortest.

You can read more about limitToFirst() in our querying data documentation.