Firebase Firestore-获取最近的趋势帖子

发布于 2025-01-28 19:58:19 字数 998 浏览 2 评论 0原文

如何使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执着的年纪 2025-02-04 19:58:19

不是最好的解决方案,而是对我有用:

import _ from "lodash";

const MIN_NUMBER_OF_LIKES_FOR_BEING_TRENDING = 2000;
const MAX_TRENDING_POSTS_TO_RETRIEVE = 10;

async function getTrendingPosts(
  currentUserId
  limit = MAX_TRENDING_POSTS_TO_RETRIEVE,
) {
  const minLikes = _.random(
    MIN_NUMBER_OF_LIKES_FOR_BEING_TRENDING,
    MIN_NUMBER_OF_LIKES_FOR_BEING_TRENDING * _.random(1, 10)
  );

  const maxLikes = _.random(
    minLikes + 1, 
    (minLikes + 1) * _.random(1, 100)
  );

  const query = firestore
    .collectionGroup("userPosts")
    .where("totalLikes", ">=", minLikes)
    .where("totalLikes", "<=", maxLikes)
    .orderBy("totalLikes", "desc")
    .orderBy("date", "desc");

  const querySnapshot = await query.limit(limit).get();

  const posts = await Promise.all(
    querySnapshot.docs.map((postDoc) => parsePost(postDoc, currentUserId))
  );

  return posts;
}

Not the best solution, but works for me:

import _ from "lodash";

const MIN_NUMBER_OF_LIKES_FOR_BEING_TRENDING = 2000;
const MAX_TRENDING_POSTS_TO_RETRIEVE = 10;

async function getTrendingPosts(
  currentUserId
  limit = MAX_TRENDING_POSTS_TO_RETRIEVE,
) {
  const minLikes = _.random(
    MIN_NUMBER_OF_LIKES_FOR_BEING_TRENDING,
    MIN_NUMBER_OF_LIKES_FOR_BEING_TRENDING * _.random(1, 10)
  );

  const maxLikes = _.random(
    minLikes + 1, 
    (minLikes + 1) * _.random(1, 100)
  );

  const query = firestore
    .collectionGroup("userPosts")
    .where("totalLikes", ">=", minLikes)
    .where("totalLikes", "<=", maxLikes)
    .orderBy("totalLikes", "desc")
    .orderBy("date", "desc");

  const querySnapshot = await query.limit(limit).get();

  const posts = await Promise.all(
    querySnapshot.docs.map((postDoc) => parsePost(postDoc, currentUserId))
  );

  return posts;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文