Condividi tramite


$minN (espressione di matrice)

SI APPLICA A: VCore mongoDB

L'operatore $minN restituisce i valori più piccoli di una matrice. È utile quando si desidera trovare gli elementi con prestazioni più basse in base a valori numerici, ad esempio le cifre di vendita più piccole o le percentuali di sconto più basse.

Sintassi

La sintassi per l'operatore $objectToArray è la seguente:

{
  $minN: {
    input: <array>,
    n: <number>
  }
}

Parametri

Descrizione
input Matrice da cui restituire i valori più piccoli. La matrice deve contenere valori numerici.
n Numero di valori più piccoli da restituire. Deve essere un intero positivo.

Esempio

Comprendiamo l'utilizzo con un esempio di json dal dataset stores.

{
  "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
  "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
  "sales": {
    "totalSales": 151864,
    "salesByCategory": [
      {
        "categoryName": "Sound Bars",
        "totalSales": 2120
      },
      {
        "categoryName": "Home Theater Projectors",
        "totalSales": 45004
      },
      {
        "categoryName": "Game Controllers",
        "totalSales": 43522
      },
      {
        "categoryName": "Remote Controls",
        "totalSales": 28946
      },
      {
        "categoryName": "VR Games",
        "totalSales": 32272
      }
    ]
  },
  "staff": {
    "totalStaff": {
      "fullTime": 19,
      "partTime": 20
    }
  }
}

Esempio 1: Ottenere i due valori di vendita più bassi

Si supponga di voler trovare i due valori di vendita più bassi di tutte le categorie di vendita per identificare i prodotti sottoperformi.

db.stores.aggregate([
  { $match: {"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"} },
  {
    $project: {
      name: 1,
      lowestTwoSales: {
        $minN: {
          input: "$sales.salesByCategory.totalSales",
          n: 2
        }
      }
    }
  }
])

In questo modo viene generato l'output seguente:

[
  {
    _id: '40d6f4d7-50cd-4929-9a07-0a7a133c2e74',
    name: 'Proseware, Inc. | Home Entertainment Hub - East Linwoodbury',
    lowestTwoSales: [ 2120, 28946 ]
  }
]

Esempio 2: Trovare le percentuali di sconto più piccole

È anche possibile usare $minN per trovare le percentuali di sconto più piccole di un evento promozionale.

db.stores.aggregate([
  { $match: {"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"} },
  { $unwind: "$promotionEvents" },
  { $match: {"promotionEvents.eventName": "Major Bargain Bash"} },
  {
    $project: {
      name: 1,
      eventName: "$promotionEvents.eventName",
      smallestDiscounts: {
        $minN: {
          input: "$promotionEvents.discounts.discountPercentage",
          n: 3
        }
      }
    }
  }
])

In questo modo vengono restituite le tre percentuali di sconto più piccole dell'evento promozionale "Major Bargain Bash".

[
  {
    _id: '40d6f4d7-50cd-4929-9a07-0a7a133c2e74',
    name: 'Proseware, Inc. | Home Entertainment Hub - East Linwoodbury',
    eventName: 'Major Bargain Bash',
    smallestDiscounts: [ 7, 8, 9 ]
  }
]

Esempio 3: Confrontare i numeri di personale tra i negozi

È possibile usare $minN per analizzare i livelli di personale trovando negozi con il numero di personale più basso.

db.stores.aggregate([
  {
    $project: {
      name: 1,
      staffNumbers: ["$staff.totalStaff.fullTime", "$staff.totalStaff.partTime"],
      lowestStaffCount: {
        $minN: {
          input: ["$staff.totalStaff.fullTime", "$staff.totalStaff.partTime"],
          n: 1
        }
      }
    }
  },
  { $limit: 5 }
])

Questa query mostra il numero di personale più basso (full-time o part-time) per ogni negozio, consentendo di identificare i negozi con personale minimo.

[
  {
    _id: 'af9015d8-3f6b-455f-8967-a83cc22ff018',
    name: 'VanArsdel, Ltd. | Party Goods Nook - Kunzeshire',
    staffNumbers: [ 15, 1 ],
    lowestStaffCount: [ 1 ]
  },
  {
    _id: 'ed319c06-731d-45fc-8a47-b05af8637cdf',
    name: 'Relecloud | Computer Outlet - Langoshfort',
    staffNumbers: [ 10, 3 ],
    lowestStaffCount: [ 3 ]
  },
  {
    _id: '62438f5f-0c56-4a21-8c6c-6bfa479494ad',
    name: 'First Up Consultants | Plumbing Supply Shoppe - New Ubaldofort',
    staffNumbers: [ 20, 18 ],
    lowestStaffCount: [ 18 ]
  },
  {
    _id: '71c50be7-5c69-4a01-9218-e479fdeb6cee',
    name: 'Wide World Importers | Carpets Market - Port Newtonburgh',
    staffNumbers: [ 1, 14 ],
    lowestStaffCount: [ 1 ]
  },
  {
    _id: '4dc0275d-b554-4b0a-a1b2-0f14154be71d',
    name: 'VanArsdel, Ltd. | DJ Equipment Outlet - Lake Edmond',
    staffNumbers: [ 2, 13 ],
    lowestStaffCount: [ 2 ]
  }
]

Esempio 4: Identificare le categorie sottoperformi in tutti i negozi

Trovare i due valori di vendita inferiori in tutti i negozi per identificare in modo coerente le categorie di prodotti con prestazioni inferiori.

db.stores.aggregate([
  { $match: { "sales.salesByCategory": { $exists: true, $ne: [] } } },
  {
    $project: {
      name: 1,
      location: 1,
      bottomTwoSales: {
        $minN: {
          input: "$sales.salesByCategory.totalSales",
          n: 2
        }
      }
    }
  },
  { $sort: { "bottomTwoSales.0": 1 } },
  { $limit: 3 }
])

Questa query restituisce i tre negozi con i valori minimi complessivi delle vendite, consentendo di identificare le posizioni che potrebbero richiedere più supporto o strategie di prodotto diverse.

[
  {
    _id: 'c601ced7-d472-47e8-91c1-f213e3f60250',
    name: 'Tailwind Traders | Bed and Bath Bazaar - West Imaniside',
    location: { lat: -41.113, lon: -108.3752 },
    bottomTwoSales: [ 101, 12774 ]
  },
  {
    _id: '09782c05-a134-43a1-a65b-6a332bc89d7c',
    name: 'Tailwind Traders | Microphone Deals - Sonnytown',
    location: { lat: -61.9575, lon: 55.2523 },
    bottomTwoSales: [ 102, 18531 ]
  },
  {
    _id: '57303916-24f1-43a9-a50c-b96fb76ae40c',
    name: 'Fabrikam, Inc. | Art Supply Boutique - Port Geovanni',
    location: { lat: 63.9018, lon: -125.7517 },
    bottomTwoSales: [ 102, 6352 ]
  }
]