So I'm having issues performing a Query to return just two values, MIN and MAX for a field inside an Array field...
client.files.uploads is the field I want to get the MIN/MAX.
The result will be just "0,10" for example, I don't want to get more fields, just look into every client.files.uploads field and get the MIN/MAX uploads whole clientcollection has, but return only the lowest and highest value not a row for each client.
This is my query, but is not getting me what I want...
db.client.aggregate([
{$unwind: "$files"},
{
$group: {
_id : "$_id",
data_min: {$min: "$files.uploads"},
data_max: {$max: "$files.uploads"}
}
},
{
$project:{
data_min: "$min",
data_max: "$max",
}
}
],{allowDiskUse:true})
UPDATE:
I've managed to do what I wanted, but the question still open because I don't thing my way is the best way to achieve it...
db.clients.aggregate([
{$unwind: "$files"},
{
$group: {
_id: "$_id",
data_min: {$min: "$files.uploads"},
data_max: {$max: "$files.uploads"}
}
},
{
$group: {
_id: "1",
min: {$min: "$data_min"},
max: {$max: "$data_max"}
}
},
{
$project: {
_id: 0,
min: 1,
max: 1
}
}
],
{
allowDiskUse: true
})
This query is returning me one row only, with the lowest and highest value for the whole collection, which is what I wanted.
This is an example data of my documents
[
{
"name": "John",
"files": [
{
"uploads": 9685,
"downloads": 83,
},
{
"uploads": 1,
"downloads": 833
},
{
"uploads": 676,
"downloads": 823
}
]
},
{
"name": "Peter",
"files": [
{
"uploads": 32,
"downloads": 99
},
{
"uploads": 34,
"downloads": 4
}
]
},
{
"name": "Mery",
"files": [
{
"uploads": 3,
"downloads": 244
},
{
"uploads": 15,
"downloads": 543
},
{
"uploads": 1345,
"downloads": 22
},
{
"uploads": 6743,
"downloads": 87543
}
]
}
]