简体   繁体   English

使用meteor-autocomplete搜索同一集合的多个字段?

[英]Search multiple fields of the same collection using meteor-autocomplete?

I am using mizzao/meteor-autcomplete in order to return matched items from a MongoDB collection when you type a query. 我正在使用mizzao / meteor-autcomplete ,以便在您键入查询时从MongoDB集合返回匹配的项目。

I am able to successfully do this if I only search for one field, but it does not let me search multiple fields of the same collection. 如果我仅搜索一个字段,那么我能够成功完成此操作,但是它不允许我搜索同一集合的多个字段。

I attempted to do it this way: 我试图这样做:

Template.foo.helpers({
    settings: function() {
            return {
                position: 'bottom',
                limit: 5,
                rules: [
                     {
                         collection: Models,
                         field: 'name',
                         matchAll: true,
                         template: Template.standardLegends,
                    },
                    {
                        collection: Models,
                        field: 'createdBy',
                        matchAll: true,
                        template: Template.standardLegendsOwner,
                    },
                ],
            };
        },
});

I looked through github open issues and there was one similar issue where they say to use the selector function in order to return multiple fields but I wasn't really able to understand what I am supposed to do. 我翻阅了github上的未解决问题,有一个类似的问题,他们说要使用选择器函数才能返回多个字段,但是我真的无法理解我应该做什么。

Here is a link to that thread: https://github.com/mizzao/meteor-autocomplete/issues/13 这是该线程的链接: https : //github.com/mizzao/meteor-autocomplete/issues/13

Any help would be appreciated. 任何帮助,将不胜感激。

Further Explanation 进一步说明

Here is an example of a dataset that is returned from my Models Collection: 这是从我的模型集合返回的数据集的示例:

0: 
    _id:"KwTZmBC5qBcwK7kzR"
    name:"drillCase.stl"
    units:"mm"
    createdBy:"Bob M"
    createdDate:"Tue Aug 29 2017 03:19:10 GMT-0400 (EDT)"
1:
    _id:"ljknbgwlkejb56"
    name:"cone.stl"
    units:"mm"
    createdBy:"Michael C"
    createdDate:"Tue Aug 29 2017 03:45:10 GMT-0400 (EDT)"

Here is the template I am currently using to display the autocomplete results: 这是我当前用于显示自动完成结果的模板:

<template name="standardLegendsOwner">
    <span class="fieldName" style="width: 500px">{{name}}</span>
</template>

So let's say I want to return all of the items that are associated with the 'createdBy' value 'Bob M'. 假设我要返回与“ createdBy”值“ Bob M”关联的所有项目。 Right now if I type Bob M, I get returned back 'drillCase.stl' instead because we are searching by the 'name' field and in the template we are return the 'name' value. 现在,如果我输入Bob M,则会返回“ drillCase.stl”,因为我们正在按“名称”字段进行搜索,而在模板中,我们将返回“名称”值。

So my question is, is it possible to setup up a conditional statement inside of the template to say something like, if the field you matched was 'name' return name values, otherwise return 'createdBy' values. 所以我的问题是,是否可以在模板内部设置条件语句,例如,如果您匹配的字段是“名称”,则返回名称值,否则返回“ createdBy”值。

The issue I get right now is, if I type in Bob M. and he is associated with more than one object, then it return first value that matches with him. 我现在遇到的问题是,如果我输入Bob M.并且他与多个对象相关联,那么它将返回与他匹配的第一个值。

The selector function basically needs to return the query object that is to be used by .find() . selector函数基本上需要返回将由.find()使用的query对象。 To look at multiple fields you will also need $or: 要查看多个字段,您还需要$or:

Template.foo.helpers({
  settings() {
    return {
      position: 'bottom',
      limit: 5,
      rules: [
        {
           collection: Models,
           field: 'name',
           matchAll: true,
           template: Template.standardLegends,    
           selector(match) {
             regex = new RegExp(match, 'i')
             return {$or: [{'name': regex}, {'createdBy': regex}]}
           },
         }
       ],
     };
   },
});

I read the documentation for mizzao:autocomplete a bit more and realized that it actually returns all the fields from the matching documents. 我阅读了mizzao:autocomplete的文档,并意识到它实际上返回了匹配文档中的所有字段。 This means you have a lot of flexibility in your results template. 这意味着结果模板具有很大的灵活性。 For example you can do: 例如,您可以执行以下操作:

<template name="standardLegendsOwner">
  <span class="fieldName" style="width: 500px">{{name}} - {{createdBy}}</span>
</template>

Since you know one of those fields will be a match. 由于您知道这些字段之一将是一个匹配项。 You could also write a helper that creates exactly what you want to see. 您也可以编写一个帮助程序,该帮助程序可以精确创建您想看到的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM