It is relatively easy to keep fields private, even if they are part of a database query. The last argument to self.added is the object passed to the client, so you can delete / modify / delete the fields that you send to the client.
The version of your violin has been changed here. This should do what you ask. (Honestly, I'm not sure why you had something in the chain after the observeChanges function in your fiddle, so maybe I donβt understand you, but looking at the rest of your question, it should be like that. Sorry if I got it wrong.)
var self = this; // Modify the document we are sending to the client. function filter(doc) { var length = doc.item.length; // White list the fields you want to publish. var docToPublish = _.pick(doc, [ 'someOtherField' ]); // Add your custom fields. docToPublish.itemLength = length; return docToPublish; } var handle = myCollection.find({}, {fields: {item:1, someOtherField:1}}) // Use observe since it gives us the the old and new document when something is changing. // If this becomes a performance issue then consider using observeChanges, // but its usually a lot simpler to use observe in cases like this. .observe({ added: function(doc) { self.added("myCollection", doc._id, filter(doc)); }, changed: function(newDocument, oldDocument) // When the item count is changing, send update to client. if (newDocument.item.length !== oldDocument.item.length) self.changed("myCollection", newDocument._id, filter(newDocument)); }, removed: function(doc) { self.removed("myCollection", doc._id); }); self.ready(); self.onStop(function () { handle.stop(); });
source share