Meteor:如何在Meteor上实现对集合的文本搜索以及其他查找查询?

时间:2014-08-17 21:30:33

标签: meteor iron-router

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

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

Notes存储在使用Schema的集合中:

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

在我的路由器中(使用铁路由器)我使用数据功能将用户注释数据返回到页面:

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

然后在玉中我迭代笔记并显示它们

 each notes
   +note

但是现在我想要包含搜索功能,以便用户可以搜索他们自己的笔记。

我想搜索每个Note的html字段。但我想通过threadId字段过滤Notes。

基本上是这样的

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]

1 个答案:

答案 0 :(得分: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