Search code examples
javascriptarraysmongodbmeteormeteor-helper

Proper MongoDB collection structure including array of values (without keys) in Meteor


I am trying to put the values of a "tags" array from MongoDB into a drop down box (select box, as options). I am using Meteor.

I have a data structure like this:

{ "_id" : ObjectId("1..."),
  "catagory" : "todo",
  "tags" : ["todonow", "todolater", "toread", "toresearch"]
}

{ "_id: : ObjectId("2..."),
  "category" : "learning",
  "tags" : ["til", "reference", "cs50", "codeacademy"]
}

I am failing to understand how to retrieve individual values from the arrays in the first place. I have seen posts on this site and others indicating I should use something like:

{{#each tags}}  //iterate through "tags" helper from java script
  {{> tag}}  //and use the "tag" template to show the items from the helper
{{/each}}

My helper does a Tags.find() and allows me to use {{category}} and {{tags}} in my "tag" template, but {{tags}} is the whole array instead of the values within each tags array.

Also, all examples I've seen have been arrays of key value pairs, not just values. My questions are as follows:

1) Is the structure I am using a valid MongoDB structure?

2) Is there a better structure for this purpose?

3) How can I ask for single values in the tags arrays and use them as options in select boxes?

I realize this is supposed to be very simple. I have searched extensively, and if there is a post or resource out there that clears this up I have not found it or unfortunately not understood it. If anyone can point me in the right direction I would really appreciate it.


Solution

  • To get values you have to do :

    {{#each tags}}
       <li>{{this}}</li>
    {{/each}}
    

    Solution was here : Meteor Handlebars: How to access a plain array?