Tシャツの通販サイトを運営しています。T シャツには 8 種類のバリエーションがあり、そのすべてが「エディション」 (または衣料品ライン) の一部です。エディションの一部であるすべてのシャツを常に表示できるようにしたいのですが、それぞれの在庫があるかどうかを確認して、それを示すことができるようにしたいと考えています.
したがって、現在、シャツ スキーマとエディション スキーマの 2 つのスキーマがあります。それらは(マングースで)次のようにモデル化されています。
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 枚存在するかどうかを確認します。
これは多くのループのように思えますが、もっと簡単な方法があるかどうか疑問に思っています。