Can Collection transforms be performed asynchronously?


I would like to be able to use async/await with Mongo.Collection’s transform option.

Is this possible?


I suspect the answer is yes and no :slight_smile:.

Yes, because you can of course, define an async transform function and include await statements within that. In other words it’s valid syntax.

No, because of this from the docs:

transform functions are not called reactively. If you want to add a dynamically changing attribute to an object, do it with a function that computes the value at the time it’s called, not by computing the attribute at transform time.

I think that even if the document is transformed after it’s been retrieved (which would be the expected behaviour for an async function), there would be no reactive change you could pick up to see the transformed document. The transform method does not affect the cursor, only the object retrieved from the cursor - and that won’t be re-retrieved if the document is changed asynchronously, because the context will have long gone by then.