hasanaktasTRH
Convex Community2y ago
4 replies
hasanaktasTR

Filtering out results

Hello. I have a query filtering structure that I want to ask about.

   const likedProducts = await ctx.db
      .query("productLikes")
      .withIndex("userId", (q) => q.eq("userId", ctx.user._id))
      .collect();
    const dislikedProducts = await ctx.db
      .query("productDislikes")
      .withIndex("userId", (q) => q.eq("userId", ctx.user._id))
      .collect();

    const likedProductIds = likedProducts.map((like) => like.productId);
    const dislikedProductIds = dislikedProducts.map(
      (dislike) => dislike.productId,
    );
    
    const  notInProductIds= [...likedProductIds, ...dislikedProductIds]; 



What is the correct way to perform a query request from the products table without these ids?
I will bring them in sets of 10. That's why filtering is needed on the db side.


Should the query be written like this?
  const products = await ctx.db
      .query("products")
      .withIndex("by_id")
      .filter((q) =>
        q.and(...notInProductIds.map((id) => q.neq(q.field("_id"), id))),
      )
      .take(10)
Was this page helpful?