This is MongoDB's api:
db.foo.watch([{$match: {"bar.baz": "qux" }}])
Let's say that collection foo
contains millions of documents. The arguments passed into watch
indicate that for every single document that gets updated the system will filter the ones that $match
the query (but it will be triggered behind the scenes with any document change).
The problem is that as my application scales, my listeners will also scale and my intuition is that I will end up having n^2 complexity with this approach.
I think that as I add more listeners, database performance will deteriorate due to changes to documents that are not part of the $match
query. There are other ways to deal with this, (web sockets & rooms) but before prematurely optimizing the system, I would like to know if my intuition is correct.
Actual Question:
Can I attach a listener to a single document, such that watch's
performance isn't affected by sibling documents?
When I do collection.watch([$matchQuery])
, does the MongoDB driver listen to all documents and then filters out the relevant ones? (this is what I am trying to avoid)