繁体   English   中英

收集字段中的流星计算值

[英]Meteor Calculated Value in Collection Field

有没有一种方法可以在Meteor收集字段中获得始终计算的值? 我目前正在开发一个用于管理三明治库存的应用。 每个三明治都可以取决于其他系列的成分。 我需要一个字段始终自动计算为库存中最低的成分数。 我怎样才能做到这一点? 我在Google上找不到任何相关信息,Meteor是否可能对此没有任何支持?

这听起来像是收集挂钩的工作 集合挂钩允许您在插入/更新集合等之前/之后执行操作。

假设您有一个食材收藏。 也许该成分集合具有类似以下的模式:

Ingredients = new Mongo.Collection('ingredients');

IngredientsSchema = new SimpleSchema({
  "name": {
    type: String
  },
  "quantity": {
    type: Number
  }
});

Ingredients.attachSchema(IngredientsSchema);

然后,您将获得一个带有假设模式的三明治集合:

Sandwiches = new Mongo.Collection('sandwiches');

SandwichesSchema = new SimpleSchema({
  "name": {
    type: String
  },
  "ingredients": {
    type: [String],
    label: "An array of ingredient internal ids (_id)"
  },
  "quantity": {
    type: Number
  }
});

Sandwiches.attachSchema(SandwichesSchema);

您的收藏夹将类似于以下内容:

Ingredients.after.update(function(userId, doc, fieldNames, modifier, options) {
  // Find the ingredient with the lowest value
  ingredient = Ingredients.findOne({}, { sort: { quantity: 1 } });
  if(ingredient && ingredient._id == doc._id) {
    //If the ingredient matches this ingredient, update all sandwiches who have the agreement to reflect the remaining quantity of ingredients.
    Sandwiches.update({ ingredients: doc._id }, { $set: { quantity: doc.quantity } }, { multi: true });
  } 
});

插入成分后,您可能还需要一个收集钩,但这应该足以帮助您入门。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM