Mongodb Check Compare Array Values True or False Table Header

I am creating an Application in Meteor for Trend Analysis.

I got a Mongodb collection Images with more then 1200 images. This images contains a description, a specified color, and a Tag Array.

Single Image Structure:

{
  "_id": "oHnr8hkAPNqSirERk",
  "color:": "Red",
  "tags": [
    "oyoSwm2K8d9YtsZgW",
    "WYsiCBTZBu3Fs9ifu",
    "QhtDXDh5wBucQmBeP"
  ],
  "description": "Image description"

}

I got another Mongodb Collection excisting of 46 Tags. This tags only contain the _id and the name of the tag.

My problem is that i want to create a dataset for machine learning prototyping. I created a table with the following headers:

|ID|Color|Tagname1|Tagname2|Tagname3|Tagname4|till Tagname46 with each handlebar

What i want to achieve for all 1200 Images is to ask a Mongodb Query if the current tag in its array, is the value of the Tagname (or the ID of the tag) so i can specify a 1 to a table cell for This image got this tag, or a 0 for this image does not have this tag. So i finaly got an output like:

|ID|Color|Tagname1|Tagname2|Tagname3|Tagname4|till Tagname46
| 1|Red | 0 | 1 | 0 | 1 |

I created the Table Headers of my Meteor App with the Handlebar helpers:

Like:

    <thead>
  <tr>
    <th>ID</th>
    <th>Color</th>
    {{#each tag in getTagNames}}
    <th>{{tag._id}}</th>
    {{/each}}
  </tr>
</thead>

The Template Helpers that I defined:

 getFewImages: function(){
     return Images.find({});
  },
  getTagNames: function(){
     return Tags.find({});
  },

I guess that I need to use a Mongo query so I can aggegrate in the Array values, but I am really stuck that I don’t know what to do anymore.

My table body where I want the magic True or False happening looks like this now:

 <tbody>
  {{#each image in getFewImages}}
    <tr>
      <td>{{image.bsrColor}}</td>
      {{#if this.tags}}
        <td>1</td>
      {{else}}
        <td>0</td>
      {{/if}}
    </tr>
  {{/each}}
</tbody>

I really hope someone can help me out, and if I am not specific enough that I can further enhance my Question to achieve what I want!

I also asked this Question on Stack Overflow, but no one have replied :frowning:
Kind Regards!

If you want to filter your Collection to only return images that has a certain tag, you can make a query like this:

var tag = 'oyoSwm2K8d9YtsZgW';
Images.find({tags:tag});

(When matching against an array field, Mongo will return true if any of the elements in the array match)

But if you want to return all images, and highlight some of them based on if they have a tag, you can create a template helper like this:

hasTag(){
  var tag = Session.get('selectedTag');
  return this.tags.indexOf(tag) !== -1;
}
<td>{{#if hasTag}}1{{else}}0{{/if}}</td>

Hey There,

Thanks for your reply.

I have been trying some different stuff now. The problem is that my table headers are existing of tag names from an other helper. (this helper is just a simple Mongo Query to find all tags). I wanted to make a TagArray excisting of all tags, and try the next function to iterate trough all image tags:

  tagRobot: function(tags){
    tagNames = Tags.find({}).fetch();

    if(tags !== null){
      for(var i = 0; i < tags.length; i++)
      {
        if(Images.findOne({tags: tagNames[i]._id})){
          return "1";
        }
      }
    }
    else {
      return "0";
    }
  },

The problem is, I get a 1 back for Images Having Tags, but I want them to place underneath the same Column as the table header (with that Tag id or name). Now my application just fills the first table data cell elements with the amount of tags it has found in the query:

Problem defined below

I hope you can help me out :slight_smile: !

Try this :slight_smile:

Helper

hasTags(){  
  var tagNames = Tags.find().fetch();
  return _.map(tagNames,(tagName)=>{
    return _.contains(this.tags,tagName._id) ? 1 : 0;
  });
}

Template

{{#each getFewImages}}
  <tr>
    <td>{{bsrColor}}</td>
    {{#each hasTags}}
      <td>{{this}}</td>
    {{/each}}
  </tr>
{{/each}}

Got the feeling I am almost there!

Only getting Zero’s back now … Wish I suddenly saw these magic ones appear for the tags a image own :stuck_out_tongue:

Oh, in my helper, it should be return _.contains(this.tags, tagName._id)

After a lot of trying (and chaning my table structure) Because i wanted to loop trough my

{{#each image in getFewImages}}
<td>{{image.bsrColor}}</td>
{{/each}}

instead of`

{{#each getFewImages}}
  <tr>
    <td>{{bsrColor}}</td>
    {{#each hasTags}}
      <td>{{this}}</td>
    {{/each}}
  </tr>
{{/each}}


I can say you really made my day!

Thank you very much. I hope that this is going to work, and this will be a lesson for me to learn some more of the javascript classes like _contains and map…

Kind Regards!

_.map and _.contains are from the Underscore library btw (included with Meteor by default). I just use them by habit. :smiley:

But for general JS you can just replace them with .map() and .indexOf(value) !== -1

1 Like