I have an app and users can create their own private Notes.
I am trying to implement a search function for these notes.
Notes are stored in a collection with Schema:
note =
threadId: params.threadId
date: date
userId: user._id,
html: params.html
In my router (using iron-router) I use the data function to return users note data to the page:
Notes.find({threadId: @params._id})
Then in jade I iterate over the note and show them
each notes
+note
But Now I want to include a search function so the user can search their own notes.
I want to search the html field of each Note. But I want to also filter the Notes by threadId field.
Basically something like this
Notes.find({threadId: @params._id}).search('test', field: {html: true})
Which finds all notes with the specific threadId field, and then searches the html field of those notes with the query term 'test'
And then once I find them, how do I update the page with the new data?
--------------------------------------UPDATE -----------------------------------------
So I got searching working, but I loose reactivity on the notes. Which is kind of a bummer. I am looking for a better way to do this.
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]
You can use regular expression ($regex) in your selector
notes = Notes.find({
threadId: @researchThread._id,
html: {$regex: 'philosophy'}
},
{
sort: {date: -1}
})
To make it reactive, you need Deps.autorun
Deps.autorun ->
notes = Notes.find({
threadId: @researchThread._id,
html: {$regex: 'philosophy'}
},
{
sort: {date: -1}
})
/* Put your render things here */
The api is changed, used Tracker.autorun
instead