Passing a function as a minimongo selector

Reading the minimongo code, it suggests that the selector of a minimongo find() can be an arbitrary js function. I hope this is true!

Can someone give a simple example using this feature. I especially want to know what parameters the function expects, and what the function is supposed to return.


OK. I worked it out

Instead of using normal mongo selectors, you can select documents from your minimongo collection by passing a function as the selector. For example: MyCollection.find(function() {… return result}); The find function then examines all the documents in the collection by calling your function for each document. ‘this’, in the function, is the document being tested. Return true if you want this document to be selected by the find function. This maintains reactivity per document as expected.

This can be especially handy if you have complex filters than cannot be easily expressed in mongo selector format, such as testings requiring a join, for example.

Warning: this feature is currently undocumented, at least as far as I can see.


I need to add a small addendum here. Find works as documented, but Blaze sometimes has an issue displaying the results using #each. It doesn’t seem to cleanup old records if the result set is reduced in length.
oddly, find(function()… ).fetch() with #each in Blaze does work though.

Perhaps a possible cause for the problem with each might be something related to the issue described here:

If for some reason your queries result in data which do not include the _id field, then each might have trouble reacting to changes in the data. Does not seem very likely, especially since you mention that fetch() works fine, but still, might be worth a quick look.

The results in both cases do include the _id field, but I expect blaze is not handling the selector case correctly, and ignores cleanup in this situation. I’ll be looking through the blaze code to see if I can find anything.

I have determined a workaround.