mongodb - 聚合嵌套数组元素
问题描述
我在聚合方面遇到了麻烦。我有这些字段的集合“站”:
stationName: string,
systemName: string,
commodities:[{
name: string,
buyPrice: number,
sellPrice: number,
stock: number,
demand: number
}]
我需要查询特定商品的最高最低价格。例如:我有商品“水”,需要在所有站点条目中获得最高的 sellPrice。
提前致谢
解决方案
您可以尝试使用/进行aggregate
查询,$max
$min
db.collection.aggregate([
{
$project: {
stationName: true,
systemName: true,
maxBuyPrice: {
$max: "$commodities.buyPrice"
},
maxSellPrice: {
$max: "$commodities.sellPrice"
},
}
}
])
对于最低价格,您可以在查询中替换$max
为$min
,或者如果这是您想要的,则包括两者
推荐阅读
- java - 如何让我的代码计算正确位置值的数量?
- php - 如何使这个 php 重定向不会被 Nginx 冲突
- java - 在java中将十六进制转换为十进制
- xamarin.forms - 是否可以延迟加载不需要的 PRISM / Xamarin Forms 组件?
- hash - H (x ◦ id) ∈ Y1,其中 id = 100 位值且 Y1 = 1000 位值或其中 id = 1000 位值且 Y2 = 100 位值。更长的时间?
- python - 如何在 Python 3 中的 set 中的特定位置/索引中添加值?
- vhdl - VHDL 如何确保使用尽可能小的加法器?
- algorithm - 找到符合特定标准的长度为 n 的一组膳食的算法
- c - 创建一个设置了 N 个最低有效位的掩码
- python-3.x - 模块“tkinter”没有属性“Treeview”