Mongodb Meteor.users中的用户名全文搜索
我正在尝试按用户名搜索meteor.users集合 我已经遵循了所有详细的步骤,但我似乎无法让它工作Mongodb Meteor.users中的用户名全文搜索,mongodb,meteor,full-text-search,meteor-accounts,Mongodb,Meteor,Full Text Search,Meteor Accounts,我正在尝试按用户名搜索meteor.users集合 我已经遵循了所有详细的步骤,但我似乎无法让它工作meteor.users 以下是我的代码: 服务器启动时: Meteor.startup(function(){ Meteor.users._ensureIndex({ "username":"text", }); }); 在我的发布功能中: Meteor.publish("Meteor.users.userSearch",function(searchVal){ if(!
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:{$search:{$some_test”}})
,但仍然没有返回任何集合对象。确保索引:
自3.0.0版以来已弃用:db.collection.ensureIndex()现在是db.collection.createIndex()的别名
_ensureIndex似乎是一个孤儿
Meteor.startup(function(){
Meteor.users.createIndex({
"username":"text"
});
});
如果只想搜索一个字段的值(
username,在本例中为),则无需进行全文搜索。在这种情况下,使用正则表达式值作为搜索值的普通find
命令更好:
Meteor.users.find({
username: new RegExp('user1', 'gi'),
});
你的代码似乎还可以。我认为问题在于你的搜索值,Mongo搜索单词或短语而不是字符你是否尝试过将你的子对象移出你的助手:Template.Template.onCreated(function(){this.autorun(function(){var searchValue=Session.get('searchVal');Meteor.subscribe('Meteor.users.userSearch',searchValue);})})@Khang-ouh是的,这就是问题所在,谢谢你弄明白了!一旦我写下完整的用户名,它就会弹出。。。你知道一种按角色搜索的方法吗?(我试图做的是动态搜索,searchVal
通过keyup
事件更新!)@MathieuK。是的,一开始我有这个想法,我认为这就是问题所在,所以我试着直接将它移动到助手中!(为示例编写的内容也更简洁)我认为使用正则表达式搜索值进行正常的find
操作就足够了