Is an all documents query the best way to listen for changes in one particular document?

I’m making an android app. I have 3 views for the different needs of my app, but in a certain Activity there is just one document and I’d like to know if making an All documents query to keep it updated is the best option or there is an alternative I’m not seeing

Just to be clear, you’re suggesting using an all docs query to avoid needing another view?

If so, I’d say it’s a tradeoff. With an all docs query, you can still apply filters. If these can’t reduce the results down to the one you want to receive, you’ll have the overhead of getting the callbacks and needing to filter the results yourself. That gets into app specific details, so you’ll have to decide for yourself. (Happy to provide more advice if you give more detail.) It does save you the overhead of having another view.

On the flip side, having a view that selects out a single document is pretty light weight. And (just as a reminder) you would not normally emit the whole document. The memory overhead should be quite small.
Overall processing overhead I think will be less. Especially if you’re looking at doing a live query, I think creating a view for this instance would be preferable.

Just to be clear, you’re suggesting using an all docs query to avoid needing another view?

Exactly.

It’s like a detail view of an object. Say I have a list of documents that I can search for, and when I click on one it opens up a new fragment with the info. I want to know if making an all docs query and filtering for the specific document ID is better than making a View that emmits all of the documents’ IDs with a specific type and query it for the specific document ID

On the flip side, having a view that selects out a single document is pretty light weight. And (just as a reminder)

What do you mean exacly by this? I know this would be efficient for a static document that always has the same Id but I’m talking about basically any document, sorry if that wasn’t clear at first

Yes, I was wondering. It did seem like you meant literally one document. No matter.

If you know the specific document ID, you should just do a straight retrieval of it. No need for a query at all.

Views are static indexes that get built up. So updating a view only takes as much processing as there are new
documents to go through since the last time it happened.

The storage footprint of a view is directly related to what you emit to it. So if you can do what you want by only emitting a small value, it can be quite small even for a large number of documents. Since you get the document ID for free with a view, in many cases you don’t need to emit anything.

I have the document id and so I retrieve the document from the database, but I want it to be updated and so I need a LiveQuery (I don’t think there is another option?) I’m using couchbase mobile 1.4 for android btw.

Right now I use a LiveQuery of an all documents query and filter it by the document Id. This seems to work ok but I’m afraid of it being too resource-consuming if I ever have multiple unrelated fragments like this, each with an all documents query at the same time

Ah, I see, you want dynamic updates. You can actually do this with a document change listener as an alternative. See https://developer.couchbase.com/documentation/mobile/1.5/references/couchbase-lite/couchbase-lite/document/document/index.html and look for addChangeListener.

1 Like

Thank you! This is exactly what I need