Meteor Collection Transform:是在服务器端完成还是在客户端完成?或者这取决于 [英] Meteor Collection Transform: is it done on the server or on the client? or it depends
问题描述
我想使用转换从集合中创建一个虚拟字段".但是,我添加的新字段(在转换函数中)正在向返回的文档添加相当多的数据.
I want to use transform to make a "virtual field" out of a collection. However, the new field I'm adding (within the transform function) is adding quite a bit of data to the returned document.
如果转换发生在客户端内部,这很好.如果是在服务器端完成,那么就会有带宽问题.
This is fine if the transform is taking place inside the client. If it is done on server-side, then there will be bandwidth concerns.
所以我想知道转换是在服务器上完成的,还是在客户端上完成的,还是取决于我如何查找/获取文档?
So I'm wondering if the transform is done on the server, or on the client, or it depends on how I find/fetch the document?
推荐答案
UPDATE:可以在服务器上进行转换.
UPDATE: It's possible to do a transform on the server.
您可以像这样在客户端上进行转换:
You can have a transform on the client like this:
return YourCollection.find({}, {transform: function (doc) {
doc.test = true;
return true;
}});
Meteor 忽略已发布的查询的 transform
(从 Meteor.publish
中).客户端将文档视为不存在转换.
Meteor ignores transform
on queries that are published (from within Meteor.publish
). The client sees the document as if the transform didn't exist.
如果您想在服务器上使用转换,您可以这样做:
If you would like to use transforms on the server you can do this:
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();
});
这篇关于Meteor Collection Transform:是在服务器端完成还是在客户端完成?或者这取决于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!