考虑以下 MongoDB“食谱”集合:
{
"title" : "Macaroni and Cheese",
"ingredients" : [
{ "name" : "noodles", "qty" : "2 c" },
{ "name" : "butter", "qty" : "2 tbl" },
{ "name" : "cheese", "qty" : "1 c" },
]
},
{
"title" : "Pound Cake",
"ingredients" : [
{ "name" : "sugar", "qty" : "1 lb" },
{ "name" : "butter", "qty" : "1 lb" },
{ "name" : "flour", "qty" : "1 lb" },
]
},
{
"title" : "Dough",
"ingredients" : [
{ "name" : "water", "qty" : "2 c" },
{ "name" : "butter", "qty" : "8 tbl" },
{ "name" : "flour", "qty" : "1 lb" },
]
}
我希望编写一个查询来生成要购买的物品的“购物清单”以制作所有食谱。所以我基本上想退回配料“面条”、“黄油”、“奶酪”、“糖”、“黄油”、“面粉”、“水”。我不想重复。(例如,糖和黄油出现在不止一个食谱中,但我只想退回一次,即没有重复。)
是否可以在 MongoDB 中创建这样的查询,如果可以,该查询会是什么?还是需要我为“成分”创建一个单独的集合?