DaReal
DaReal8mo ago

Difference Between .get and .query and Best Use Case

Hello! I am trying to understand the difference between the ctx.db.get and the ctx.db.query I have read the docs but i am a bit confused. I also think they can be used to perform the same operation in some cases. However, I would like a good explanation and best use case for each. Thanks in advance! ☺️
4 Replies
lee
lee8mo ago
db.get is for looking up a single document by its _id field. db.query is for any other kind of query: by some index, by a text search index, or a table scan with a filter.
DaReal
DaRealOP8mo ago
Does that mean db.get will return the whole data of the document also? And if i add the document _id value, both db.get and db.query will work right?
lee
lee8mo ago
Yes both db.get and db.query return whole documents. And yes you can replicate a db.get(messageId) with a db.query("messages").withIndex("by_id", q=>q.eq("_id",messageId)).unique(). db.get is syntactic sugar
DaReal
DaRealOP8mo ago
Oh! Okay nice

Did you find this page helpful?