适用对象:
MongoDB vCore
运算符 $minN
从数组中返回 n 个最小值。 如果要根据数值(如最小销售数字或最低折扣百分比)查找性能最低的项,则非常有用。
语法
$minN
运算符的语法如下:
{
$minN: {
input: <array>,
n: <number>
}
}
参数
DESCRIPTION | |
---|---|
input |
要从中返回 n 个最小值的数组。 数组应包含数值。 |
n |
要返回的最小值数。 必须是正整数。 |
示例:
让我们了解 stores
数据集中的示例 json 的用法。
{
"_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
}
}
}
示例 1:获取最低两个销售值
假设你想要查找所有销售类别中最低的两个销售值,以识别表现不佳的产品。
db.stores.aggregate([
{ $match: {"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"} },
{
$project: {
name: 1,
lowestTwoSales: {
$minN: {
input: "$sales.salesByCategory.totalSales",
n: 2
}
}
}
}
])
这会生成以下输出:
[
{
_id: '40d6f4d7-50cd-4929-9a07-0a7a133c2e74',
name: 'Proseware, Inc. | Home Entertainment Hub - East Linwoodbury',
lowestTwoSales: [ 2120, 28946 ]
}
]
示例 2:查找最小折扣百分比
还可以用于 $minN
从促销活动中查找最小折扣百分比。
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
}
}
}
}
])
这将从“主要讨价还价 Bash”促销活动返回三个最小折扣百分比。
[
{
_id: '40d6f4d7-50cd-4929-9a07-0a7a133c2e74',
name: 'Proseware, Inc. | Home Entertainment Hub - East Linwoodbury',
eventName: 'Major Bargain Bash',
smallestDiscounts: [ 7, 8, 9 ]
}
]
示例 3:比较各商店的员工人数
可以通过 $minN
查找具有最低员工计数的商店来分析人员配备级别。
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 }
])
此查询显示每个商店的最低员工计数(全职或兼职),帮助识别人员配备最少的商店。
[
{
_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 ]
}
]
示例 4:识别所有商店中表现不佳的类别
在所有商店中查找底部的两个销售值,以识别一致表现不佳的产品类别。
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 }
])
此查询返回具有总最低最低销售值的三家商店,帮助确定可能需要更多支持或不同产品策略的位置。
[
{
_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 ]
}
]