我想使用转换从集合中创建一个“虚拟字段”。但是,我添加的新字段(在转换函数中)正在向返回的文档中添加相当多的数据。
如果转换发生在客户端内部,这很好。如果在服务器端完成,则会出现带宽问题。
所以我想知道转换是在服务器上还是在客户端上完成,还是取决于我如何查找/获取文档?
最佳答案
更新:可以在服务器上进行转换。
您可以像这样在客户端进行转换:
return YourCollection.find({}, {transform: function (doc) {
doc.test = true;
return true;
}});
Meteor 忽略对已发布查询的transform
(来自Meteor.publish
)。客户端看到文档时就好像转换不存在一样。
如果您想在服务器上使用转换,您可以这样做:
YourCollection = new Mongo.Collection("collection_name");
Meteor.publish("yourRecordSet", function() {
//Transform function
var transform = function(doc) {
doc.date = new Date();
return doc;
}
var self = this;
var observer = YourCollection.find().observe({
added: function (document) {
self.added('collection_name', document._id, transform(document));
},
changed: function (newDocument, oldDocument) {
self.changed('collection_name', newDocument._id, transform(newDocument));
},
removed: function (oldDocument) {
self.removed('collection_name', oldDocument._id);
}
});
self.onStop(function () {
observer.stop();
});
self.ready();
});
关于javascript - Meteor Collection Transform : is it done on the server or on the client? 或者它取决于,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18093560/