从内部数组返回具有最低值的每个项目 [英] Return Each Item with the Lowest Value from the Inner Array
问题描述
对我来说,我是在考虑逻辑,以便仅返回具有最低价值主张的每个项目(如果是json)。
{
要求者:测试,
订单项:[
{
描述: testitem1,
提案:[
{
公司: company1,
值:10
},
{
公司: company2,
值:5
}
]
}
]
}
取决于可用的MongoDB服务器版本:
您可以使用 $ indexOfArray
,以MongoDB 3.4作为最简单的方法:
db.getCollection('collection')。aggregate([
{ $ addFields:{
orderitems:{
$ map:{
input: $ orderitems,
as: o,
in:{
description: $$ o.description,
proposals:{
$ arrayElemAt:[
$$ o.proposals,
{ $ indexOfArray:[
$$ o .proposals.value,
{ $ min: $$ o.proposals.value}
]}
]
}
}
}
}
}}
])
MongoDB 3.2可以 $ filter
并使用 $ arrayElemAt
:
db.getCollection('collection')。aggregate([
{ $ addFields:{
orderitems:{
$ map:{
input: $ orderitems,
as: o,
in:{
description: $$ o.description,
提案:{
$ arrayElemAt:[
{ $ filter:{
input: $$ o.proposals ,
cond:{ $ eq:[ $$ this.value,{ $ min: $$ o.proposals.value}]}
}},
0
]
}
}
}
}
}}
])
老实说,尝试在任何低于该版本的版本中实施都是很痛苦的。
基本原理是根据 $ min
,它确实在MongoDB 3.2中有所更改,因此可用于从任何值数组中返回最小值,而不仅仅是聚合 $ group
在3.2版中进行了更改:$ min在$ group和$ project阶段可用。在以前的MongoDB版本中,$ min仅在$ group阶段可用。
因此,一般情况是我们匹配数组项通过返回的值从 $ indexOfArray
或通过 $ filter
。
根据选择的方法,索引输入到 $ arrayElemAt
提取该索引处的数组值,或者使用 0
索引从过滤列表中获取第一个元素。 / p>
这两种方法都会返回:
{
er: test,
orderitems:[
{
description: testitem1,
proposals:{
company: company2,
值:5.0
}
}
]
}
Speaking people I'm here thinking of logic to return only each item with its lowest value proposition, if json.
{
requester: "test",
orderitems: [
{
description: "testitem1",
proposals: [
{
company: "company1",
value: 10
},
{
company: "company2",
value: 5
}
]
}
]
}
Depending on your available MongoDB server version:
You either implement using $indexOfArray
with MongoDB 3.4 as the shortest way to do this:
db.getCollection('collection').aggregate([
{ "$addFields": {
"orderitems": {
"$map": {
"input": "$orderitems",
"as": "o",
"in": {
"description": "$$o.description",
"proposals": {
"$arrayElemAt": [
"$$o.proposals",
{ "$indexOfArray": [
"$$o.proposals.value",
{ "$min": "$$o.proposals.value" }
]}
]
}
}
}
}
}}
])
Or with MongoDB 3.2 you can $filter
and take the first matching element with $arrayElemAt
:
db.getCollection('collection').aggregate([
{ "$addFields": {
"orderitems": {
"$map": {
"input": "$orderitems",
"as": "o",
"in": {
"description": "$$o.description",
"proposals": {
"$arrayElemAt": [
{ "$filter": {
"input": "$$o.proposals",
"cond": { "$eq": ["$$this.value",{ "$min": "$$o.proposals.value" }] }
}},
0
]
}
}
}
}
}}
])
And it honestly would be a pain to try and implement in any version lower than that.
The essential principle is matching based on the value returned by $min
, which did get a change in MongoDB 3.2 so it can be used to return the "minimum" value from any array of values rather than just as an "accumulator" in aggregation $group
Changed in version 3.2: $min is available in the $group and $project stages. In previous versions of MongoDB, $min is available in the $group stage only.
So the general case is we match the array entry by the returned value either from the matching index of $indexOfArray
or by whatever is returned as matching array entries of $filter
.
Depending on the method chosen, either the 'matched index' is fed to $arrayElemAt
to extract the array value at that index, or the 0
index is used to get the "first" element from the "filtered list".
Either approach returns:
{
"requester" : "test",
"orderitems" : [
{
"description" : "testitem1",
"proposals" : {
"company" : "company2",
"value" : 5.0
}
}
]
}
这篇关于从内部数组返回具有最低值的每个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!