如何在 MeteorJs 中将 $text $search 与 _ensureIndex 和 SimpleSchema 一起使用?

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

我在 MeteorJs 中使用带有 _ensureIndex 和 SimpleSchema 的 $text $search 进行搜索时没有结果。

mycollection.js

var1: {
    type: String
  },

...

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

search.js

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

这个有效

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

有了这个,我没有结果:为什么?

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

有什么想法吗?

谢谢

试试这个:

App.Collections.Observations._ensureIndex({'var2.name': 1});

如果您希望使用同一个查询在多个字段中进行搜索,我建议:

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

我还要确保所有指定的字段都是字符串(通常如果 var2.name 是字符串,则 var2 不是 - 不确定这是否必要,具体取决于您的模式,但可能更好)