0

Okay, I have a collection in MongoDB for a meteor project. A document is too long to post here so I'm just going to put in an example of a document in the collection to get my point across:

_id: "RxBqDLxxPG89ngp9d",
name: "deck of cards",
deck: [
  {
   suit: "spades",
   value: "ace",
  },
  {
   suit: "spades",
   value: "king",
  },
  {
   suit: "spades",
   value: "queen",
  },
  {
   suit: "hearts",
   value: "ace",
  },
  {
   suit: "hearts",
   value: "king",
  },
  {
   suit: "hearts",
   value: "queen",
  }
]

(one can assume that I did the whole deck, but it doesn't matter for the purposes of the example) And so I know that if I wanted to get names of all of those on a page I would have to write a helper in whatever template I'm working with.

"displayCards": function(){ return deckOfCards.find().deck; }

And then in the html of the helper:

{{#each displayCards}}
  <div class="card">{{value}} of {{suit}}</div>
{{/each}}

Which will return:

ace of spades
king of spades
queen of spades
ace of hearts
king of hearts
queen of hearts

So here is where I finally get to my question:
I want to put a div with a class of suit around each suit of cards. To do this I need to be able to either return an array of just the elements where the suit is equal to hearts or spades, etc. OR I need to be able to return a piece of the array 13 (or two, if we are just going by what I have written) cards at a time. Is there a way to do this?

  • Your question is hard to understand. Could you please be more specific about what you are trying to do here? – styvane May 01 '16 at 20:00
  • ultimately? I want to be able to make style changes to one suit, but not the other. as it stands now I dont have a way to put the different suits in different containers because when I do {{#each}} it pulls out every element of the array. I want to be able to pull just a subset at a time. – Gregers Walker May 01 '16 at 20:36
  • I should also be clear. I cant just use $slice, because in the program I am running, there are multiple arrays in each document, so when I run deckOfCards.find({}, {deck: {$slice: 3}}) It will limit the array as I want, but it will also return everything else, and I need just this one array – Gregers Walker May 01 '16 at 21:04
  • https://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection – styvane May 01 '16 at 21:06

1 Answers1

0

This is how you can perform the operation for multiple docs.

Your helper code:-

"displayCards": function(){ 
     return deckOfCards.find()
 }

Your html code:-

{{#each displayCards}}
     {{#each deck}}
          <div class="card">{{value}} of {{suit}}</div>
     {{/each}}
{{/each}}

So you just have to pass your desk in the second #each loop. That's how you can do that.

Pankaj Jatav
  • 2,158
  • 2
  • 14
  • 22