【问题标题】:Username full text search in Meteor.usersMeteor.users 中的用户名全文搜索
【发布时间】:2017-05-30 23:59:32
【问题描述】:

我正在尝试按用户名搜索 meteor.users 集合。

我已按照here 详述的所有步骤进行操作,但我似乎无法使其工作meteor.users

这是我的代码:

在服务器启动时:

Meteor.startup(function(){
  Meteor.users._ensureIndex({
    "username":"text",
  });
});

在我的发布功能中:

Meteor.publish("Meteor.users.userSearch",function(searchVal){

  if(!searchVal){
   return Meteor.users.find({});
  }

  return Meteor.users.find({$text:{$search:searchVal}});

});

在客户端:

Template.foo.helpers({
  users(){
    var searchValue = Session.get('searchVal');
    Meteor.subscribe('Meteor.users.userSearch',searchValue);
    return Meteor.users.find({});
  }
});

谁能帮我弄清楚上面的问题是什么?

当没有searchValue 时,它工作正常并返回所有用户。只要有任何搜索值,就根本不会返回任何用户。

我也直接在mongodb控制台db.users.find({$text:{$search:"some_test"}})试过了,还是没有返回集合对象。

【问题讨论】:

  • 您的代码似乎没问题。我认为问题在于您的搜索值,Mongo 搜索单词或短语而不是字符
  • 你有没有试过把你的 sub 移到你的助手之外: Template.template.onCreated(function(){ this.autorun(function(){ var searchValue = Session.get('searchVal'); Meteor.订阅('Meteor.users.userSearch',searchValue); }) })
  • @Khang ouuh 是的,这就是问题所在,感谢您的解决!一旦我写下完整的用户名,它就会弹出......你知道按字符搜索的方法吗? (我想做的是动态搜索 searchVal 更新 keyup 事件!)
  • @MathieuK。是的,我最初有,我认为这是问题所在,所以我尝试将其直接移至帮助程序中!(也更简洁地为示例编写)
  • 我认为带有正则表达式搜索值的普通find 操作应该足够了

标签: mongodb meteor full-text-search meteor-accounts


【解决方案1】:

如果您只想搜索一个字段的值(在这种情况下为username),则不需要进行全文搜索。在这种情况下,使用带有正则表达式值作为搜索值的普通 find 命令会更好:

Meteor.users.find({
  username: new RegExp('user1', 'gi'),
});

【讨论】:

    【解决方案2】:

    确保索引: 自 3.0.0 版起已弃用db.collection.ensureIndex() 现在是 db.collection.createIndex() 的别名。

    _ensureIndex 似乎是一个孤儿。

    Meteor.startup(function(){
      Meteor.users.createIndex({
        "username":"text"
      });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-14
      • 1970-01-01
      相关资源
      最近更新 更多