I’m working on a project where an explore page requires random posts. I want to get 10 random posts from the posts collection. I am using firebase web SDK v9.6.3.
This is code I have written to get x number of posts:
export async function getRandomPosts(postCount) {
const q = query(
collection(db, "posts"),
limit(postCount)
// orderBy("timestamp", "desc")
);
const querySnapshot = await getDocs(q);
let posts = [];
querySnapshot.forEach((doc) => {
posts.push({
id: doc.id,
...doc.data(),
timestamp: doc.data().timestamp?.toDate().toString(),
});
});
return posts;
}
I can’t find anything to get the random documents. Is there anything like rand in mySQL ?