Searching over multiple collections

What is the most sensible way to search over multiple collections? I.e. I want to do

{{#each cols}}
{{/each}}

where cols is a combination of say, aCol.find(), bCol.find(), cCol.find().

The most obvious way is just fetching everything and combining it in an array, and another one is publishing a combination of different cursors (with the disadvantage of duplicate client data.

How would you do this?

All of these collections have an timestamps by the way, so being able to sort all of them collectively would also be great.

It sounds like you may have designed your database a bit awkwardly. I have a feeling you’ve previously used foreign keys in my/postegreSQL or similar?

It may sound weird but I think reimagining your DB may make things easier. Check out a few tutorials on MongoDB schema design. Doing calls to get that info will be rather slow in the long run.

If you’re doing something like a full text search, this may be of use to you:

More specifically, here: http://matteodem.github.io/meteor-easy-search/docs/javascript-api/
under (Client) searchMultiple(indexes, searchString, callback)
It allows you to create indexes which specify columns in one collection to search through. And specify an array of indexes so you can search across all indexes with one search string.

This only gives you a bunch of documents, you have to then sort it yourself, but if you’ve got timestamps already, it would be a trivial matter.

Also re ‘duplicate data’, minimongo handles multiple publishes that publish overlapping subsets of a collection or even fields within collections so you never get duplicate data on your client side. The document is either there or not, and if yes, then it either has the field or doesn’t. This is my understanding of how it works.

1 Like

Yeah, I think you are right! I recently started using SimpleSchema, and that is the reason why I work with multiple Collections now (instead of how I usually just create free form documents).

But I realised that I don’t need to attachSchemas to Collection, and hence I’ve reworked my Collection to stay away from my initial awkward design :smiley:

Also re ‘duplicate data’, minimongo handles multiple publishes that publish overlapping subsets of a collection or even fields within collections so you never get duplicate data on your client side. The document is either there or not, and if yes, then it either has the field or doesn’t. This is my understanding of how it works.

Never knew this! Makes me love Meteor more!