Aggregate occurrences of events in nested array - mongodb

Given the following input:
[
{
"statuses": [
{
"status": "allowed",
"count": 3,
"events_count": [
"2001",
"1001",
"1001"
]
}
],
"date": "2022-09-10 15:00",
"_id": "2022-09-10 15:00"
}
]
I need count the number of occurrences of stauses.events_count, so the output would be:
[
{
"statuses": [
{
"status": "allowed",
"count": 3,
"events_count": [
{"type": "2001", "count": 1},
{"type": "1001", "count": 2},
]
}
],
"date": "2022-09-10 15:00",
"_id": "2022-09-10 15:00"
}
]
What I've tried
This is what I got so far:
db.collection.aggregate([
{
"$unwind": "$statuses"
},
{
"$unwind": "$statuses.events_count"
},
{
"$group": {
"_id": {
"event_count": "$statuses.events_count",
"status": "$statuses.status",
"date": "$date",
"count": "$statuses.count"
},
"occurences": {
"$sum": 1
}
}
}
])
Which produces:
[
{
"_id": {
"count": 3,
"date": "2022-09-10 15:00",
"event_count": "2001",
"status": "allowed"
},
"occurences": 1
},
{
"_id": {
"count": 3,
"date": "2022-09-10 15:00",
"event_count": "1001",
"status": "allowed"
},
"occurences": 2
}
]
I'm having difficulties grouping everything back together. I tried grouping by date and pushing back to a 'statuses' array, but it produces two items in the array (with status==allowed), rather than 1 item with status==allowed

You did 2 $unwinds, so it should be 2 $groups in reverse order:
{
"$group": {
"_id": {
"status": "$_id.status",
"count": "$_id.count",
"date": "$_id.date"
},
"event_count": {
"$push": {
"type": "$_id.event_count",
"count": "$occurences"
}
}
}
},
{
"$group": {
"_id": "$_id.date",
"date": {"$last": "$_id.date"},
"statuses": {
"$push": {
"status": "$_id.status",
"count": "$_id.count",
"event_count": "$event_count"
}
}
}
}

Related

data stored in map, need all values of all object in an array

We have data like
[{
"parameterId": "5f914ca2679bae721d38410b",
"average": 574998.153846154,
"count": 26.0,
"date": "2020-09-08T18:30:00.000Z",
"dataPerHour": {
"0": {
"min": 92570.0,
"max": 995170.0,
"avg": 578268.826086957,
"count": 23,
"date": "2020-09-04T19:07:41.000Z",
"values": [{
"paramValue": "100414",
"time": "2020-09-04T19:07:41.000Z"
},
{
"paramValue": "705811",
"time": "2020-09-04T19:08:41.000Z"
}
]
},
"1": {
"min": 92570.0,
"max": 995170.0,
"avg": 678268.826086957,
"count": 23,
"date": "2020-09-03T19:07:41.000Z",
"values": [{
"paramValue": "100414",
"time": "2020-09-03T19:07:41.000Z"
},
{
"paramValue": "705811",
"time": "2020-09-03T19:08:41.000Z"
}
]
}
}
}, {
"parameterId": "5f914ca2679bae721d38410b",
"average": 574998.153846154,
"count": 26.0,
"date": "2020-09-08T18:30:00.000Z",
"dataPerHour": {
"0": {
"min": 92570.0,
"max": 995170.0,
"avg": 778268.826086957,
"count": 23,
"date": "2020-09-04T19:07:41.000Z",
"values": [{
"paramValue": "100414",
"time": "2020-09-04T19:07:41.000Z"
},
{
"paramValue": "705811",
"time": "2020-09-04T19:08:41.000Z"
}
]
}
}
}]
We need output:
[
"2020-09-08T18:30:00" : "578268.826086957",
"2020-09-03T19:07:41" : "678268.826086957",
"2020-09-08T18:30:00" : "778268.826086957"
]
I need mongo query for this. I need data like key = date and value = avg of each data in dataPerHour.
Does this help?
Playground
out
[
{
"data": [
{
"2020-09-03T19:07:41": 678268.826086957,
"2020-09-04T19:07:41": 578268.826086957
},
{
"2020-09-04T19:07:41": 778268.826086957
}
]
}
]
pipe
db.collection.aggregate([
{
$project: {
data: {
"$arrayToObject": {
$map: {
input: {
"$objectToArray": "$dataPerHour"
},
as: "d",
in: {
$cond: [
"$$d",
[
{
"$dateToString": {
"date": {
$toDate: "$$d.v.date"
},
"format": "%Y-%m-%dT%H:%M:%S"
}
},
{
"$toDouble": "$$d.v.avg"
}
],
"$$d"
]
}
}
}
}
}
},
{
$group: {
_id: null,
data: {
$push: "$$ROOT.data"
}
}
},
{
$unset: "_id"
}
])

MongoDB, How to associate array fields for statistics

Example JSON:
{
"groups": [
{
"_id": 1,
"name": "g1"
},
{
"_id": 2,
"name": "g2"
}
],
"items": [
{
"_id": 1,
"name": "item1",
"gid": 1
},
{
"_id": 2,
"name": "item2",
"gid": 2
}
]
}
How to associate two arrays and count ?I tried to use aggregate, I didn't get the results I wanted.
Required Result:
Or can directly find all the items associated with it, perfect....
{"groups": [
{
"_id": 1,
"name": "g1",
"count": 1,
"items": [
{
"_id": 1,
"name": "item1"
}
]
},
{
"_id": 2,
"name": "g2",
"count": 1,
"items": [
{
"_id": 2,
"name": "item2"
}
]
}
]}
db.getCollection('collection').aggregate([
{$unwind:{
path:"$groups",
preserveNullAndEmptyArrays:true
}},
{$unwind:{
path:"$items",
preserveNullAndEmptyArrays:true
}},
{$redact: {$cond: [{
$eq: [
"$groups._id",
"$items.gid"
]
},
"$$KEEP",
"$$PRUNE"
]
}
},
{$project:{
_id:1,
groups_id:"$groups._id",
group_name:"$groups.name",
item_data:{
_id:"$items._id",
name:"$items.name",
}
}},
{
$group:{
_id:"$groups_id",
name:{$first:"$group_name"},
count:{$sum:1},
items:{$push:"$item_data"}
}
}
])

MongoDB aggregate results into nested array

I'm quite new to MongoDB and I am currently facing a situation. Below are 2 sample records from the whole database that I have :
{
"_id": 1,
"Record": 1,
"Link": [ "https://wikileaks.org/plusd/cables/1979PANAMA06344_e.html" ],
"Location": [ "USA", "PAN", "USA", "USA", "PAN" ],
"Organization": [ "GN", "SOUTHCOM", "UCMJ", "PRC" ],
"Date": [ "2016" ],
"People": [ "P.Walter" ]
}
{
"_id": 2,
"Record": 2,
"Link": [ "https://wikileaks.org/gifiles/docs/11/111533_-latam-centam-brief-110822-.html" ],
"Location": [ "NIC", "GTM", "JAM", "GTM", "PAN" ],
"Organization": [ "CENTAM", "Calibre Mining Corporation", "STRATFOR", "Alder Resources" ],
"Date": [ "2013" ],
"People": [ "Daniel Ortega", "Hugo Chavez", "Paulo Gregoire" ]
}
Basically, I'm trying to get an output like this :
{
"Country": "US",
"Years": [
{
"Year": "2016",
"Links": [ "https://wikileaks.org/gifiles/docs/11/111533_-latam-centam-brief-110822-.html",
"https://wikileaks.org/plusd/cables/1979PANAMA06344_e.html",
"https://wikileaks.org/gifiles/docs/90/9058_wax-12312008-csv-.html" ]
},
{
"Year": "2013",
"Links": [ ""https://wikileaks.org/gifiles/docs/11/111533_-latam-centam-brief-110822-.html",
"https://wikileaks.org/plusd/cables/1979PANAMA06344_e.html",
"https://wikileaks.org/gifiles/docs/90/9058_wax-12312008-csv-.html" ]
}
]
"Link_Count": 6
}
{
"Country": "UK",
"Years": [
{
"Year": "2009",
"Links": [ "https://wikileaks.org/gifiles/docs/11/111533_-latam-centam-brief-110822-.html",
"https://wikileaks.org/plusd/cables/1979PANAMA06344_e.html",
"https://wikileaks.org/gifiles/docs/90/9058_wax-12312008-csv-.html" ]
},
{
"Year": "2011",
"Links": [ ""https://wikileaks.org/gifiles/docs/11/111533_-latam-centam-brief-110822-.html",
"https://wikileaks.org/plusd/cables/1979PANAMA06344_e.html"]
}
]
"Link_Count": 5
}
I've tried to aggregate it, but I couldn't achieve what I want like I've given in the output. Here's my query :
db.test.aggregate([
{
"$unwind": "$Location"
},
{
"$group" : {
"_id": {
"Country": "$Location",
"Year": "$Date",
"Links": "$Link"
},
Loc: {
$addToSet: "$Location"
}
}
},
{
"$unwind": "$Loc"
},
{
"$group": {
"_id": "$Loc",
"Years": { "$push": {
"Year": "$_id.Year",
"Links": "$_id.Links"
}
}
}
}
]).toArray()
I used $unwind and $addToSet on $Location because there are duplicates found within $Location. I'm open to any suggestions or solution so please do tell! Thanks in advance!
You can use :
db.test.aggregate([{
"$unwind": "$Location"
}, {
"$unwind": "$Date"
}, {
"$unwind": "$Link"
}, {
"$group": {
"_id": {
"Country": "$Location",
"Year": "$Date"
},
Links: {
$addToSet: "$Link"
}
}
}, {
"$group": {
"_id": "$_id.Country",
Years: {
$push: {
"Year": "$_id.Year",
"Links": "$Links"
}
},
Link_Count: { $sum: { $size: "$Links" } }
}
}])
The idea is to $unwind all arrays to be able to $push link into a new array, and to count the grouped record with $size for the last $group stage.

sort a pushed array in mongodb

I have a mongodb result like this.
[
{
"_id": {
"_id": "57174838afb8eb97ccd409ca",
"name": "Yet another position",
"description": "description",
"code": "11Y-WK"
},
"votes": [
{
"candidate": {
"_id": "56f19694e84a6bf1b66ad378",
"surname": "XXXXXXXXX",
"firstName": "XXXXXXXXX",
"middleName": "XXXXXXXXX",
"othername": " XXXXXXXXX XXXXXXXXX",
"sc_number": "071050"
},
"count": 3
},
{
"candidate": {
"_id": "56f19690e84a6bf1b66aa558",
"surname": "XXXXXXXXX",
"othername": "XXXXXXXXX XXXXXXXXX",
"sc_number": "034837"
},
"count": 2
},
{
"candidate": {
"_id": "56f19690e84a6bf1b66aa2f3",
"surname": "XXXXXXXXX",
"othername": "XXXXXXXXX XXXXXXXXX",
"sc_number": "008243"
},
"count": 4
}
],
"total_count": 9
},
{
"_id": {
"_id": "571747a8afb8eb97ccd409c7",
"name": "Test Position",
"description": "Description",
"code": "10T-9K"
},
"votes": [
{
"candidate": {
"_id": "56f19690e84a6bf1b66aa3b7",
"surname": "XXXXXXXXX",
"othername": "XXXXXXXXX",
"sc_number": "044660"
},
"count": 1
},
{
"candidate": {
"_id": "56f19690e84a6bf1b66aa6ea",
"surname": "XXXXXXXXX",
"othername": "XXXXXXXXX",
"sc_number": "062444"
},
"count": 5
},
{
"candidate": {
"_id": "56f1968fe84a6bf1b66aa03e",
"surname": "XXXXXXXXX",
"othername": "XXXXXXXXX",
"sc_number": "042357"
},
"count": 3
}
],
"total_count": 9
}
]
I need to sort by count in descending order.
Below is the query that returns the result above.
I've tried applying sort at all stages but with no luck.
Vote.aggregate([
{ "$match": { "_poll" : mongoose.mongo.ObjectID(req.query._poll) } },
{
"$group": {
"_id": {
"_position": '$_position',
"candidate": '$candidate'
},
"voteCount": { "$sum": 1 }
}
},
{
"$group": {
"_id": "$_id._position",
"votes": {
"$push": {
"candidate": "$_id.candidate",
"count": "$voteCount"
}
},
"total_count": { "$sum": "$voteCount" }
}
},
{ "$sort": { "total_count": -1 } }
Where I need to insert sort operation, to sort by count in descending order.
To sort an array during aggregation it is a bit tricky stuff.
So what I am duinfg to solve that is:
unwinding array
apply sort
regroup array
please find mongo shell code which will should give an overview ot this technique:
var group = {$group:{_id:"$type", votes:{$push:"$value" }}}
var unwind={$unwind:"$votes"}
var sort = {$sort:{"votes":-1}}
var reGroup = {$group:{_id:"$_id", votes:{$push:"$votes" }}}
db.readings.aggregate([group,unwind,sort ,reGroup])
any comments welcome!

MongoDB projection. Operator $add field|expression array awareness or after $slice

I've got collection that looks like:
[{
"org": "A",
"type": "simple",
"payFor": 3,
"price": 100
},
{
"org": "A",
"type": "custom",
"payFor": 2,
"price": 115
},
{
"org": "B",
"type": "simple",
"payFor": 1,
"price": 110
},
{
"org": "B",
"type": "custom",
"payFor": 2,
"price": 200
},
{
"org": "B",
"type": "custom",
"payFor": 4,
"price": 220
}]
And need to produce result with query to perform group by "org" where payments appears for only first "payFor" prices in "type".
I'm trying to use expression result by $slice operator in $add but this is not works.
pipeline:
[{
"$group": {
"_id": {
"org": "$org",
"type": "$type"
},
"payFor": {
"$max": "$payFor"
},
"count": {
"$sum": 1
},
"prices": {
"$push": "$price"
}
}
},
{
"$group": {
"_id": "$_id.org",
"payments": {
"$push": {
"type": "$_id.type",
"forFirst": "$payFor",
"sum": {
"$cond": [
{
"$gte": [
"$payFor",
"$count"
]
},
{
"$add": {
"$prices": {
"$slice": "$count"
}
}
},
{
"$add": "$prices"
}
]
}
}
}
}
}]
I know that it is possible to traverse unwinded prices and pick only "payFor" count of them. but result collections are more rich than in example above and this operation will produce some unecessary overheads.
Need some advice from community. Please. Thanks.