好的,我已经找到了一种方法来做到这一点,而无需重新完成整个数据库。流星棒极了!
在服务器上:
Meteor.publish('search_results', function(query){
if(query){
var self = this;
var actors = Actors.find({ $or : [{ name : query}, { actor_biography : query }] ),
films = Films.find({ $or : [{ name : query}, { actor_biography : query }] ),
cinemas = Cinemas.find({ $or : [{ name : query}, { actor_biography : query }] );
actors.forEach(function(doc){
self.added('search_collection', doc._id, { name : doc.name, type : 'actor' });
});
films.forEach(function(doc){
self.added('search_collection', doc._id, { name : doc.name, type : 'film' });
});
cinemas.forEach(function(doc){
self.added('search_collection', doc._id, { name : doc.name, type : 'cinema' });
this.ready();
} else {
this.ready();
}
});
在客户端:
Deps.autorun(function(){
Meteor.subscribe('search_results', Session.get('currentQuery'));
});
SearchCollection = new Meteor.Collection('search_collection');
现在,SearchCollection 包含我想要从结果中获得的数据,我可以在其中分别决定想要从每个集合中获得哪些数据。
一个缺点是我显然在重复数据 - 一些(如果不是全部)这些记录已经存在于客户端上......