【问题标题】:Retrieve items from mongodb collections if an element of an array is present (meteor js)如果存在数组元素(meteor js),则从 mongodb 集合中检索项目
【发布时间】:2016-07-07 12:49:55
【问题描述】:

只有当数组中的元素等于我存储在会话中的当前月份时,我才想从集合中检索元素。例如,如果我的会话变量等于“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);
            });
       }
    });
}

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

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

我在模板之后使用 blaze:

{{#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}}

谢谢

约安

【问题讨论】:

    标签: javascript meteor meteor-blaze helpers


    【解决方案1】:
    Legumes.find({ moisrec: "juillet" });
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-24
      • 2018-04-13
      相关资源
      最近更新 更多