如果存在数组元素,则从mongodb集合中检索项目(meteor js)

时间:2022-04-04 17:12:25

I would like to retrieve elements from a collection only if an element in an array is equal to the current month that I have stored in a session. For example, if my session variable is equal to "juillet" I would like to have only the items which have "juillet" in the "moisrec" method :

我想仅当数组中的元素等于我在会话中存储的当前月份时才从集合中检索元素。例如,如果我的会话变量等于“juillet”,我想只有“moisrec”方法中有“juillet”的项目:

Legumes = new Mongo.Collection("legumes");

if (Meteor.isServer) {
    Meteor.startup(function() {
      if (Legumes.find().count() === 0) {
            var legumes = [{
                nom: "poireau",
                moisrec: ["juillet", "août"],
            }, {
                nom: "navet",
                moisrec: ["octobre", "novembre"],
            }, {
                nom: "choux-fleur",
                moisrec: ["juillet", "août"]
            }];
            // Insert sample data into db.
            legumes.forEach(function(item) {
                Legumes.insert(item);
            });
       }
    });
}

I have a helpers that looks like this :

我有一个看起来像这样的助手:

Template.Legumes.helpers({
  legumes : function() {
    return Legumes.find({});
  }
});

I use blaze after for templating :

我使用火焰进行模板化:

{{#each legumes}}
  <div class="col-sm-3">
      <div class="thumbnail">
        <img src="legumes/{{nom}}.jpg" alt="{{nom}}" width="400" height="300">
         <p><strong>{{nom}}</strong></p>
         <p>Période récolte : {{#each mois in moisrec}}<a>{{mois}} </a>{{/each}}</p>
         <button class="btn" href="/legumes:{{_id}}">Fiche complète</button>
      </div>
   </div>
 {{/each}}

Thanks

Yoann

1 个解决方案

#1


0  

Legumes.find({ moisrec: "juillet" });

That should get you what you need. Since it's a simple array of strings there isn't any other fancy calls you'll need to make.

这应该可以满足您的需求。由于它是一个简单的字符串数组,因此您不需要进行任何其他花哨的调用。

#1


0  

Legumes.find({ moisrec: "juillet" });

That should get you what you need. Since it's a simple array of strings there isn't any other fancy calls you'll need to make.

这应该可以满足您的需求。由于它是一个简单的字符串数组,因此您不需要进行任何其他花哨的调用。