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
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.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?
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 sugarOh! Okay nice