Meteor.js-在多个集合中进行用户搜索的方法 [英] Meteor.js - ways to do user search over multiple collections

查看:88
本文介绍了Meteor.js-在多个集合中进行用户搜索的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于在Meteor应用程序中如何设置收藏集,我有些困惑.用户搜索栏是我应用程序的中心部分,用户需要能够输入一个搜索并在多个不同的集合中获得结果:

user query : 'foo'

var query = 'foo';
var actors_results = Actors.find({ $or : [{ name : query}, { actor_biography : query }] );
var films_results = Films.find({ $or : [{ name : query}, { description : query }] );
var cinemas_results = Cinemas.find({ $or : [{ name : query}, { genres : query }] );

对于搜索结果显示,这不是很好-我不认为(???)我可以将这些光标组合成一个光标,因此分页之类的简单操作几乎变得不可行(或者我不知道该怎么做)去做).

我知道的唯一解决方案是制作一个SuperCollection,在其中放置所有文档并具有一个类型"字段,该字段可以是演员",电影"或电影",然后分页等变得很容易. /p>

但是在我看来,这确实使其他所有事情变得复杂...数据库的结构不再反映数据.演员和电影院在数据/结构方面没有共同点,我只需要能够并行搜索它们即可.

我还将如何根据字段'type'的值为SuperCollection设置多个验证方案?

任何想法/建议都值得赞赏!

解决方案

好的,我已经找到了一种无需重新做整个数据库的方法.真棒流星!

在服务器上:

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包含我要从结果中获取的数据,在这里我可以分别决定要从每个集合中获取哪些数据.

缺点之一是我显然在复制数据-如果不是所有这些记录中的某些记录已经存在于客户端上...

I'm having a bit of a dilemma concerning how to set up the collections in my Meteor app. The user search bar is a central part of my app, and the user needs to be able to enter one search and have results across several different collections :

user query : 'foo'

var query = 'foo';
var actors_results = Actors.find({ $or : [{ name : query}, { actor_biography : query }] );
var films_results = Films.find({ $or : [{ name : query}, { description : query }] );
var cinemas_results = Cinemas.find({ $or : [{ name : query}, { genres : query }] );

For search results display this is not good - I don't think (???) I can combine these cursors into one cursor, and so simple things like pagination become almost impossible to do (or I don't know how to do it).

The only solution I know would be to make a SuperCollection where I put all of the documents and have a field 'type' which could be 'actor', 'film' or 'cinema', then pagination etc becomes easy.

But it seems to me this really complicates everything else... the structure of the database no longer reflects the data. Actors and Cinemas have nothing in common in terms of data/structure, I just need to be able to search them in parallel.

Also how would I set up multiple validation schemes for the SuperCollection depending on the value of the field 'type' ?

Any ideas/suggestions much appreciated !!!

解决方案

OK I've worked out a way to do this without re-doing my entire database. Bravo Meteor !

on the server :

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();
    }
});

and on the client :

Deps.autorun(function(){
    Meteor.subscribe('search_results', Session.get('currentQuery'));
});

SearchCollection = new Meteor.Collection('search_collection');

Now SearchCollection contains the data I want from the results, where I can decide what data I want from each collection separately.

One disadvantage is that I'm clearly duplicating data - some if not all of these records exist on the client already...

这篇关于Meteor.js-在多个集合中进行用户搜索的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆