$addToSet 演算子は、要素がまだ存在しない場合は配列に追加し、セット内の要素の一意性を確保します。
構文
{
$addToSet: { <field1>: <value1> }
}
パラメーター
| パラメーター | Description |
|---|---|
<field1> |
要素を追加するフィールド。 |
<value1> |
配列に追加する値。 |
例示
stores コレクションのこのサンプル ドキュメントについて考えてみましょう。
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
"location": {
"lat": -89.2384,
"lon": -46.4012
},
"staff": {
"totalStaff": {
"fullTime": 8,
"partTime": 20
}
},
"sales": {
"totalSales": 75670,
"salesByCategory": [
{
"categoryName": "Wine Accessories",
"totalSales": 34440
},
{
"categoryName": "Bitters",
"totalSales": 39496
},
{
"categoryName": "Rum",
"totalSales": 1734
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 7,
"Day": 2
}
},
"discounts": [
{
"categoryName": "Whiskey",
"discountPercentage": 7
},
{
"categoryName": "Bitters",
"discountPercentage": 15
},
{
"categoryName": "Brandy",
"discountPercentage": 8
},
{
"categoryName": "Sports Drinks",
"discountPercentage": 22
},
{
"categoryName": "Vodka",
"discountPercentage": 19
}
]
},
{
"eventName": "Steal of a Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Organic Wine",
"discountPercentage": 19
},
{
"categoryName": "White Wine",
"discountPercentage": 20
},
{
"categoryName": "Sparkling Wine",
"discountPercentage": 19
},
{
"categoryName": "Whiskey",
"discountPercentage": 17
},
{
"categoryName": "Vodka",
"discountPercentage": 23
}
]
}
]
}
例 1: tag 配列に新しいタグを追加する
このクエリは、タグの配列に新しいタグを追加し、$addToSet演算子を使用してクエリを実行して新しい値を追加します。
db.stores.updateOne({
_id: "0fcc0bf0-ed18-4ab8-b558-9848e18058f4"
}, {
$addToSet: {
tag: "#ShopLocal"
}
})
このクエリは、次の結果を返します。
[
{
"acknowledged": true,
"insertedId": null,
"matchedCount": "1",
"modifiedCount": "0",
"upsertedCount": 0
}
]
例 2: promotionEvents 配列に新しいプロモーション イベントを追加する
このクエリは、 promotionEvents 配列に新しいイベントを追加し、追加する新しい昇格オブジェクトと共に$addToSet演算子を使用してクエリを実行します。
db.stores.updateOne({
_id: "0fcc0bf0-ed18-4ab8-b558-9848e18058f4"
}, {
$addToSet: {
promotionEvents: {
eventName: "Summer Sale",
promotionalDates: {
startDate: {
Year: 2024,
Month: 6,
Day: 1
},
endDate: {
Year: 2024,
Month: 6,
Day: 15
}
},
discounts: [{
categoryName: "DJ Speakers",
discountPercentage: 20
}]
}
}
})
このクエリは、次の結果を返します。
[
{
"acknowledged": true,
"insertedId": null,
"matchedCount": "1",
"modifiedCount": "1",
"upsertedCount": 0
}
]
例 3 - $setWindowOperatorsでの$addToSetの使用
"First Up Consultants" 企業内の各店舗の市区町村の一覧を取得するには、会社の最初のパーティション ストアに対してクエリを実行します。 次に、$addToSet演算子を使用して、パーティション内の各ストアの個別の都市を追加します。
db.stores.aggregate([{
$match: {
company: {
$in: ["First Up Consultants"]
}
}
}, {
$setWindowFields: {
partitionBy: "$company",
sortBy: {
"sales.totalSales": -1
},
output: {
citiesForCompany: {
$push: "$city",
window: {
documents: ["unbounded", "current"]
}
}
}
}
}, {
$project: {
company: 1,
name: 1,
citiesForCompany: 1
}
}])
このクエリによって返される最初の 2 つの結果は次のとおりです。
[
{
"_id": "a1713bed-4c8b-46e7-bb68-259045dffdb4",
"name": "First Up Consultants | Bed and Bath Collection - Jaskolskiside",
"company": "First Up Consultants",
"citiesForCompany": [
"South Thelma",
"South Carmenview",
"Port Antone",
"Charlotteville",
"South Lenorafort",
"Jaskolskiside"
]
},
{
"_id": "6b8585ab-4357-4da7-8625-f6a1cd5796c5",
"name": "First Up Consultants | Computer Depot - West Zack",
"company": "First Up Consultants",
"citiesForCompany": [
"South Thelma",
"South Carmenview",
"Port Antone",
"Charlotteville",
"South Lenorafort",
"Jaskolskiside",
"West Zack"
]
}
]