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 $reverseArray viene usato per invertire l'ordine degli elementi in una matrice. Questo operatore può essere utile quando è necessario elaborare o visualizzare gli elementi della matrice nell'ordine opposto. Si tratta di un operatore di espressione di matrice e può essere usato all'interno delle pipeline di aggregazione.
Sintassi
{
$reverseArray: <array>
}
Parametri
| Parametro | Description |
|---|---|
<array> |
Matrice da invertire. |
Esempi
Si consideri questo documento di esempio dalla raccolta negozi.
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
"location": {
"lat": 60.1441,
"lon": -141.5012
},
"staff": {
"totalStaff": {
"fullTime": 2,
"partTime": 0
}
},
"sales": {
"salesByCategory": [
{
"categoryName": "DJ Headphones",
"totalSales": 35921
}
],
"fullSales": 3700
},
"promotionEvents": [
{
"eventName": "Bargain Blitz Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 3,
"Day": 11
},
"endDate": {
"Year": 2024,
"Month": 2,
"Day": 18
}
},
"discounts": [
{
"categoryName": "DJ Turntables",
"discountPercentage": 18
},
{
"categoryName": "DJ Mixers",
"discountPercentage": 15
}
]
},
{
"eventName": "Discount Delight Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 5,
"Day": 11
},
"endDate": {
"Year": 2024,
"Month": 5,
"Day": 18
}
}
}
],
"tag": [
"#ShopLocal",
"#FashionStore",
"#SeasonalSale",
"#FreeShipping",
"#MembershipDeals"
]
}
Esempio 1: Inversione dell'ordine di una matrice
Questa query illustra l'utilizzo dell'operatore per l'esecuzione dell'inversione nell'ordine della promotionEvents matrice.
db.stores.aggregate([
//filtering to one document
{
$match: {
_id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5"
}
},
{
$project: {
_id: 1,
name: 1,
promotionEventsReversed: {
$reverseArray: "$promotionEvents"
}
}
},
// Include only _id, name, promotionalDates and eventName fields in the output
{
$project: {
_id: 1,
name: 1,
"promotionEventsReversed.promotionalDates": 1,
"promotionEventsReversed.eventName": 1
}
}
])
Questa query restituisce il risultato seguente.
[
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
"promotionEventsReversed": [
{
"eventName": "Discount Delight Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 5,
"Day": 11
},
"endDate": {
"Year": 2024,
"Month": 5,
"Day": 18
}
}
},
{
"eventName": "Bargain Blitz Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 3,
"Day": 11
},
"endDate": {
"Year": 2024,
"Month": 2,
"Day": 18
}
}
}
]
}
]
Contenuti correlati
- Esaminare le opzioni per la migrazione da MongoDB ad Azure DocumentDB.
- Altre informazioni sulla compatibilità delle funzionalità con MongoDB.