Home > OS >  Firebase order and limit
Firebase order and limit

Time:11-02

I fetch from a specific collection and it have more than 100 values and I want to order them by rank. I've a rank field. Where do I put the order and limit in the following code.

useEffect(
    () =>
      onSnapshot(collection(db, "collectiondb/id/users"), (snapshot) =>
        setLeaders(snapshot.docs.map((doc) => ({ ...doc.data(), id: doc.id })))
      ),
    []
  );

Here is how it's going to output it.

  const [leaders, setLeaders] = useState([{ name: "Loading...", id: "initial" }]);

<ul type='1'>
        {leaders.map((leader) => (
          <li>
              {leader.rank} {leader.name} </>
          </li>
        ))}
</ul>

CodePudding user response:

You could use .orderBy this way:

collection(db, 'collectiondb/id/users')
.orderBy('rank')
.orderBy('createdDate', 'desc')
.limit(100)
  • Related