简体   繁体   中英

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.

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.

Here is a link to that thread: 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'. 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.

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.

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.

The selector function basically needs to return the query object that is to be used by .find() . To look at multiple fields you will also need $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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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