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 $zip viene usato per unire due o più matrici a livello di elemento in una singola matrice di matrici. È utile quando si vogliono combinare elementi correlati da più matrici in una singola struttura di matrice.
Sintassi
{
$zip: {
inputs: [ <array1>, <array2>, ... ],
useLongestLength: <boolean>, // Optional
defaults: <array> // Optional
}
}
Parametri
| Parametro | Description |
|---|---|
inputs |
Matrice di matrici da unire per elemento. |
useLongestLength |
Valore booleano che, se impostato su true, usa la lunghezza più lunga delle matrici di input. Se false o non è specificato, usa la lunghezza più breve. |
defaults |
Matrice di valori predefiniti da usare se useLongestLength è true e qualsiasi matrice di input è più breve della matrice più lunga. |
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: Utilizzo di base
Si supponga di voler unire i categoryName campi e totalSales dalla salesByCategory matrice. Questa query restituisce una singola matrice di matrici nel categoryWithSales campo.
useLongestLength impostato su true restituirà l'output seguente, mentre un valore di false rimuove la Napkins matrice dall'output.
db.stores.aggregate([{
$match: {
_id: "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6"
}
},
{
$project: {
name: 1,
categoryNames: "$sales.salesByCategory.categoryName",
totalSales: "$sales.salesByCategory.totalSales",
categoryWithSales: {
$zip: {
inputs: ["$sales.salesByCategory.categoryName", "$sales.salesByCategory.totalSales"],
useLongestLength: false
}
}
}
}
])
Questa query restituisce il risultato seguente.
[
{
"_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
"name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
"categoryNames": ["Stockings"],
"totalSales": [25731],
"categoryWithSales": [["Stockings", 25731]]
}
]
Contenuti correlati
- Esaminare le opzioni per la migrazione da MongoDB ad Azure DocumentDB.
- Altre informazioni sulla compatibilità delle funzionalità con MongoDB.