#s

3 messages · Page 1 of 1 (latest)

oak tendon
#

is it best praicetice to use query over db.get

oak sirenBOT
#

Hi @oak tendon. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

It depends what you’re doing:

  • If you already have a document’s ID and just need that one document, ctx.db.get is the recommended best practice. It’s a fast point lookup and avoids building a query at all. [Document IDs; Reading data]

    const user = await ctx.db.get("users", userId);
    
  • Use ctx.db.query when you need to read multiple documents or filter/order by fields (often via indexes). [Reading data]

    const messages = await ctx.db
      .query("messages")
      .withIndex("by_channel", q => q.eq("channel", channelId))
      .collect();
    

The API docs even say: “If you only need to load an object by ID, use db.get(id) instead.” [Query interface]

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: