【问题标题】:Meteor: How to implement text search on a collection along with other find queries in Meteor?Meteor:如何在集合上实现文本搜索以及 Meteor 中的其他查找查询?
【发布时间】:2014-10-10 19:20:24
【问题描述】:

我有一个应用程序,用户可以创建自己的私人笔记。

我正在尝试为这些笔记实现搜索功能。

Notes 存储在带有 Schema 的集合中:

note = 
  threadId: params.threadId
  date: date
  userId: user._id,
  html: params.html

在我的路由器(使用iron-router)中,我使用数据函数将用户笔记数据返回到页面:

Notes.find({threadId: @params._id})

然后在jade中我遍历笔记并显示它们

 each notes
   +note

但现在我想包含一个搜索功能,以便用户可以搜索自己的笔记。

我想搜索每个 Note 的 html 字段。但我还想按 threadId 字段过滤注释。

基本上是这样的

Notes.find({threadId: @params._id}).search('test', field: {html: true})

查找具有特定 threadId 字段的所有笔记,然后使用查询词“test”搜索这些笔记的 html 字段

然后,一旦我找到它们,如何使用新数据更新页面?

--------------------------更新 -------- ----------------------------------

所以我开始搜索工作,但我对笔记失去了反应。这有点令人失望。我正在寻找一种更好的方法来做到这一点。

 notes = Notes.find({threadId: @researchThread._id}, {sort: {date: -1}})
 $('#notes-container').empty()

 notes.forEach (note) ->
   if note.html.indexOf("philosophy") > -1
     renderedTemplate = UI.renderWithData Template.note, note
     UI.insert renderedTemplate, $('#notes-container')[0]

【问题讨论】:

    标签: meteor iron-router


    【解决方案1】:

    您可以在选择器中使用正则表达式 ($regex)

    notes = Notes.find({
                           threadId: @researchThread._id, 
                            html: {$regex: 'philosophy'}
                       }, 
                       {
                           sort: {date: -1}
                       })
    

    更新

    要使其具有响应性,您需要Deps.autorun

    Deps.autorun ->
    
        notes = Notes.find({
                           threadId: @researchThread._id, 
                            html: {$regex: 'philosophy'}
                       }, 
                       {
                           sort: {date: -1}
                       })
    
        /* Put your render things here */
    

    更新

    api改了,改用Tracker.autorun

    【讨论】:

    • 效果很好,谢谢。但我仍然在反应方面遇到问题。获得游标数据后,如何更新页面以使其仍然具有反应性?我尝试使用 UI.renderWithData 和 UI.insert 每个注释,就像在更新的示例中一样,但我失去了反应性......
    • 你需要Deps.autorun
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 2015-02-19
    • 1970-01-01
    相关资源
    最近更新 更多