Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
L'operatore $addToSet aggiunge elementi a una matrice se non esistono già, garantendo al tempo stesso l'univocità degli elementi all'interno del set.
Sintassi
{
$addToSet: { <field1>: <value1> }
}
Parametri
| Parametro | Description |
|---|---|
<field1> |
Campo a cui si vuole aggiungere gli elementi. |
<value1> |
Valore da aggiungere alla matrice. |
Esempi
Si consideri questo documento di esempio dalla raccolta negozi.
{
"_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
}
]
}
]
}
Esempio 1: Aggiungere un nuovo tag alla tag matrice
Questa query aggiunge un nuovo tag alla matrice di tag ed esegue una query usando l'operatore $addToSet per aggiungere il nuovo valore.
db.stores.updateOne({
_id: "0fcc0bf0-ed18-4ab8-b558-9848e18058f4"
}, {
$addToSet: {
tag: "#ShopLocal"
}
})
Questa query restituisce il risultato seguente:
[
{
"acknowledged": true,
"insertedId": null,
"matchedCount": "1",
"modifiedCount": "0",
"upsertedCount": 0
}
]
Esempio 2: Aggiungere un nuovo evento promozionale alla matrice promotionEvents
Questa query aggiunge un nuovo evento alla promotionEvents matrice, eseguire una query usando l'operatore $addToSet con il nuovo oggetto promotion da aggiungere.
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
}]
}
}
})
Questa query restituisce il risultato seguente:
[
{
"acknowledged": true,
"insertedId": null,
"matchedCount": "1",
"modifiedCount": "1",
"upsertedCount": 0
}
]
Esempio 3- Uso di $addToSet con $setWindowOperators
Per recuperare l'elenco delle città per ogni negozio incluso nella società "First Up Consultants", eseguire una query per suddividere prima i negozi in base alla società. Usare quindi l'operatore $addToSet per aggiungere le città distinte per ogni negozio all'interno della partizione.
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
}
}])
I primi due risultati restituiti da questa query sono:
[
{
"_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"
]
}
]