2

I'm building a page on a meteor site that lists the documents of a collection. The collection is stored in a variable called Tools and has a field named "categories". At the top of this page, I want to have buttons that correspond to the categories; when the button is active, the documents of that category show up. When a button is not active, the documents of that category do not show up.

The thing that's throwing me is that I want to set it up so that you can select multiple categories at once.

To hard code the mongodb query to return multiple category would be something like:

Tools.find(
  { $or: 
    [
      { category: 'tech' },
      { category: 'creative' } 
    ] 
  }
);

But I can't figure out how to write this query such that it updates according to Session variables.

As for the Session variables themselves, would it be better to have a boolean variable for each category? As in...

Session.set('tech', true);
Session.set('creative', false);
//etc...

Or would it be better to store them in an array? As in...

Session.set('categories', ['tech', 'creative']);

Thanks in advance!

1 Answer 1

2

A list of categories seems like it would be much easier to manage:

Session.set('categories', ['tech', 'creative']);

Then you can do this:

Tools.find({category: {$in: Session.get('categories')}});
Sign up to request clarification or add additional context in comments.

1 Comment

Killed it! Thank you. Didn't know about the $in operator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.