0

我正在开发 T 恤的电子商务网站。T 恤有 8 种变体,所有这些都是“版本”(或服装系列)的一部分。我希望能够始终展示属于该版本的所有衬衫,但能够检查每件衬衫是否有货,以便表明这一点。

所以现在我有两个模式:衬衫模式和版本模式。它们是这样建模的(在猫鼬中):

  ShirtSchema: {
    color: {
      type: String
    },
    size: {
      type: String
    },
    edition: {
      type: {} (This is where I would embed the entire edition schema)
    }
  }

  EditionSchema: {
    name: {
      type: String
    },
    dateCreated: {
      type: Date
    },
    members: {
      type: Array (This is where I would have to list one of every type of shirt)
    }
  }

所以模式的例子是:

var newEdition = new editionSchema();
newEdition.name = 'newEdition';
newEdition.dateCreated = Date.now();
newEdition.members = [{
  // List one of every single shirt. I.E. Red, Blue etc. 
}];

// Insert these into the 'shirts' collection 10 times
var redShirt = new ShirtSchema();
redShirt.color = 'red';
redShirt.size = 'Large';
redShirt.edition = newEdition;
// Insert these into the 'shirts' collection 5 times
var redShirt = new ShirtSchema();
redShirt.color = 'red';
redShirt.size = 'Medium';
redShirt.edition = newEdition;

var blueShirt = new ShirtSchema();
blueShirt.color = 'blue';
blueShirt.size = 'Medium';
blueShirt.edition = newEdition;

我希望能够将版本嵌入到衬衫对象中,但是我不确定如何做到这一点。如果我在 ShirtSchema 中嵌入版本信息,那么一旦购买了所有裤子(即从数据库中删除),我似乎将无法检索版本信息。

但是,现在看来,我检查是否有库存某种颜​​色的衬衫的唯一方法如下:

  1. 检索版本架构。
  2. 循环遍历所有成员(以便我知道该版本的每个成员)
  3. 遍历整个衬衫系列并检查每种颜色的至少一件衬衫是否存在

这似乎有很多循环,我想知道是否有更简单的方法?

4

0 回答 0