Search code examples
mongodbmeteorsimple-schema

How use $text $search with _ensureIndex and SimpleSchema in MeteorJs?


I have no result in my search using $text $search with _ensureIndex and SimpleSchema in MeteorJs.

mycollection.js

var1: {
    type: String
  },

...

var2: {
  type: new SimpleSchema({
    _id: {
      type: String        
    },
    name: {
      type: String
    }
  }),
},

search.js

results = myCollection.find({$text: {$search: searchText}});

this works

myCollection._ensureIndex({var1: 'text'});

With this, I have no result : why ?

myCollection._ensureIndex({var2: 'text'}); // here, I have no result
myCollection._ensureIndex({var2.name: 'text'}); // here, I have an error on console

Any idea ?

Thank you


Solution

  • If you want several fields to be searched in with the same query I'd suggest :

    _ensureIndex({
        "var1":"text",
        "var2.name":"text"
      })
    

    I'd also make sure that all specified fields are Strings (typically if var2.name is a String, var2 isn't - not sure this is necessary depending on your schema but probably better)