Example: insert data into empty Mongo result set
In this example, we're going to detect that a query has returned an empty result set, and add an object to that result set.
We're going to assume the use of MSG packets, which is what most modern Mongo apps use.
We can then add the code:
A MongoDB response MSG packet always has at least one section, we check that it is of type BODY.
The body normally looks like:
If the cursor.firstBatch array is empty, that's an empty result set, so we can simply add objects to it, and that's what the client will receive.
In the real world, you would probably use a duplex filter so that you can remember the last query and behave accordingly.