Transform documents on find()

Is there anyway to transform a document when returning a cursor? So for example:

 Collection.find({ }, { transform: function( doc ) {
   \\modify doc here
   return doc
 } } );

Thanks.

1 Like

Check the arguments, you will find the transform Function.

An optional transformation function. Documents will be passed through this function before being returned from fetch or findOne, and before being passed to callbacks of observe, map, forEach, allow, and deny. Transforms are not applied for the callbacks of observeChanges or to cursors returned from publish functions.

1 Like

You can also use find().map(), if you don’t want the documents from that collection to always get transformed.

I wanted to avoid doing this as I only need to modify a few documents on the server before sending them to the client.

@herteby

I actually tried this and got the error:

Publish function returned an array of non-Cursors

Yeah, .map() also converts it to an Array, there’s no other way.
Transforming documents on the server is tricky! You should probably get a package to help you. What are you trying to do, a join?
Check out Grapher maybe:

Thanks, I will check it out. Hopefully it works with the Meteor.users collection.