Firebase Firestore-获取最近的趋势帖子
如何使用Firestore查询“最近的趋势帖子”?
如果在过去的4个小时内上传,我将其视为“最新”。 如果它具有2000多个喜欢。
,我将其视为“趋势”。
我尝试了以下操作:
const MIN_NUMBER_OF_LIKES_FOR_BEING_TRENDING = 2000;
async function getRecentTrendingPosts(
limit = 10,
minimumPostsDate = diffDate(new Date(), 4, "hours"),
) {
const query = firestore
.collectionGroup("userPosts")
.where("date", ">=", minimumPostsDate)
.where("totalLikes", ">=", MIN_NUMBER_OF_LIKES_FOR_BEING_TRENDING)
.orderBy(date, "desc");
const querySnapshot = await query.limit(limit).get();
const posts = await Promise.all(
querySnapshot.docs.map((postDoc) => parsePost(postDoc))
);
return posts;
}
但是它不起作用,而且我很确定这是因为将“> =”应用于两个不同的字段。
在复合查询中,范围(<,< =,>,> =),而不是等于(!=,not-In)比较必须在同一字段上滤波。
实施此类查询的其他想法吗?
另外,我正在考虑添加
.where("__name__", ">=", uuidv4())
查询,只是为了增加一定程度的随机性。
How can I query for "recent trending posts" using Firestore?
I am considering it as "recent" if it is was uploaded in the last 4 hours.
I am considering it as "trending" if it has more than 2000 likes.
I have tried the following:
const MIN_NUMBER_OF_LIKES_FOR_BEING_TRENDING = 2000;
async function getRecentTrendingPosts(
limit = 10,
minimumPostsDate = diffDate(new Date(), 4, "hours"),
) {
const query = firestore
.collectionGroup("userPosts")
.where("date", ">=", minimumPostsDate)
.where("totalLikes", ">=", MIN_NUMBER_OF_LIKES_FOR_BEING_TRENDING)
.orderBy(date, "desc");
const querySnapshot = await query.limit(limit).get();
const posts = await Promise.all(
querySnapshot.docs.map((postDoc) => parsePost(postDoc))
);
return posts;
}
But it doesn't work, and I am pretty sure it is because of applying ">=" to two different fields.
In a compound query, range (<, <=, >, >=) and not equals (!=, not-in) comparisons must all filter on the same field.
Any other idea for implementing this type of query?
Also, I was thinking about adding
.where("__name__", ">=", uuidv4())
to the query, just to add a degree of randomness.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不是最好的解决方案,而是对我有用:
Not the best solution, but works for me: