コレクション内の文書構造cities
はこんな感じ
都市
{
_id: ObjectId("5e78ec62bb5b406776e92fac"),
city_name: "Mumbai",
...
...
subscriptions: [
{
_id: 1,
category: "Print Magazine",
subscribers: 183476
options: [
{
name: "Time",
subscribers: 56445
},
{
name: "The Gentlewoman",
subscribers: 9454
},
{
name: "Gourmand",
subscribers: 15564
}
...
...
]
},
{
_id: 2,
category: "RSS Feed",
subscribers: 2645873
options: [
{
name: "Finance",
subscribers: 168465
},
{
name: "Politics",
subscribers: 56945
},
{
name: "Entrepreneurship",
subscribers: 56945
},
...
...
]
}
]
}
ユーザーが以下のように購読すると
{
cityId: 5e78ec62bb5b406776e92fac
selections: [
{
categoryId: 1,
options : ["Time", "Gourmand"]
},
{
categoryId: 2,
selected: ["Politics", "Entrepreneurship"]
}
]
}
cities
ドキュメントの以下を更新したい
- 「プリントマガジン」の購読者数を1増やす
- 「時間」の登録者数を 1 増やす
- 「グルメ」の登録者数を 1 増やす
- 「RSS フィード」の購読者数を 1 増やす
- 「政治」の登録者数を 1 増やす
- 「アントレプレナーシップ」の登録者数を 1 増やす
そのため、アイテムがサブスクライブされると、そのサブスクライバー カウントが 1 増加します。また、アイテムが属するカテゴリのサブスクライバー カウントも 1 増加します。
これを単一の更新クエリで実現したいと考えています。どうすればこれを行うことができますか?
ユースケースの詳細
各ユーザーのサブスクリプションの詳細はuser_subscription_details
コレクションに保存されます (ここには記載されていません)。subscriptions
のプロパティはcities
、各都市のサブスクリプションの概要のみを保持します。