2017-01-16 5 views
1

私はmeteor.usersコレクションをユーザ名で検索しようとしています。Meteor.usersでユーザー名全文を検索

hereの手順をすべて実行しましたが、うまく動作しないようです。meteor.usersここで

そのために私が持っているコードです:

は、サーバー起動時に:クライアントで

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({}); 
    } 
}); 
私の公開機能で

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

誰かが嘆願できますか?私は上記の何が間違っているのか理解してもらえますか?

searchValueがない場合、正しく動作し、すべてのユーザーが返されます。検索値があるとすぐに、ユーザーはまったく返されません。

また、mongodbコンソールdb.users.find({$text:{$search:"some_test"}})で直接試してみましたが、やはりコレクションオブジェクトが返されません。

+1

あなたのコードは大丈夫のようです。私は問題があなたの検索価値であると思う、Mongoは文字でない単語またはフレーズを検索する – Khang

+0

あなたの助手の外であなたのサブを動かしてみましたか: Template.template.onCreated(this.autorun(function(){ Meteor.subscribe( 'Meteor.users.userSearch'、searchValue); }) –

+0

@Khang ouuhはい、それを理解していただきありがとうございます!完全なユーザー名を書き留めると、すぐにポップアップします...文字で検索する方法を知っていますか? (私がしようとしているのは、 '' '' '' 'keyup'''イベントで' 'searchVal'''が更新される動的検索です!) – EugVal

答えて

1

あなただけの唯一のフィールド(この場合はusername)の値を検索したい場合は、全文検索を行う必要はありません。検索値として正規表現の値で、通常のfindコマンドを使用すると、このケースで優れている:

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

ensureIndex: バージョン3.0.0撤廃db.collection.ensureIndex()は現在db.collection.createIndex()の別名です。

_ensureIndexは孤立しているようです。

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