I want to be able to add a new field in aggregation and return the average value of all records with the same session ID. Something like this, but it's not possible to use a groupby inside addfield:
{
$addFields: {
sessionData: {
$group: {
_id: {
"sessionId": "$sessionId"
},
avgScrollDepth: { $avg: "$scrollDepthChange" },
totalSessionLength: { $max: "$scrollDepthChange" }
}
}
}
},
Edit:
Lets say these are the documents:
{
"sessionId": 1,
"sessionDepth": 1
},
{
"sessionId": 1,
"sessionDepth": 2
},
{
"sessionId": 1,
"sessionDepth": 3
}
I would want to return documents like this:
{
"sessionId": 1,
"sessionDepth": 1,
"totalSessionLength": 3
},
{
"sessionId": 1,
"sessionDepth": 2,
"totalSessionLength": 3
},
{
"sessionId": 1,
"sessionDepth": 3,
"totalSessionLength": 3
}
You can do it this way:
We store inside tmp variable all grouped documents
Count totalSessionLength
Flatten tmp variable with $unwind operator
Add totalSessionLength into final result
db.collection.aggregate([
{
$group: {
_id: "$sessionId",
totalSessionLength: {
$sum: 1
},
tmp: {
$push: {
"sessionId": "$sessionId",
"sessionDepth": "$sessionDepth"
}
}
}
},
{
$unwind: "$tmp"
},
{
$project: {
"_id": 0,
"sessionId": "$tmp.sessionId",
"sessionDepth": "$tmp.sessionDepth",
"totalSessionLength": "$totalSessionLength"
}
}
])
MongoPlayground
Generic way:
db.collection.aggregate([
{
$group: {
_id: "$sessionId",
totalSessionLength: {
$sum: 1
},
tmp: {
$push: "$$ROOT"
}
}
},
{
$unwind: "$tmp"
},
{
$replaceRoot: {
newRoot: {
$mergeObjects: [
"$tmp",
{
totalSessionLength: "$totalSessionLength"
}
]
}
}
}
])
MongoPlayground
Related
Let's say I have the input docs below:
[
{
"_id": "6225ca4052e7c226e2dd836d",
"data": [
"07",
"07",
"12",
"19",
"07",
"32"
]
},
{
"_id": "6225ca4052e7c226e2dd888f",
"data": [
"99",
"97",
"52",
"99",
"58",
"92"
]
}
]
I want to count the occurrences of every element in data string array per document. In JS, I can use countBy. How can I achieve the same using MongoDB Aggregation Framework?
I have tried to $reduce but MongoDB seems to not support assigning dynamic field to object.
{
$reduce: {
input: '$data',
initialValue: {},
in: { // assign `$$this` with count to `$$value`, but failed! }
}
}
Below is the desired output.
[
{
"_id": "6225ca4052e7c226e2dd836d",
"freqs": {
"12": 1,
"19": 1,
"32": 1,
"07": 3
}
},
{
"_id": "6225ca4052e7c226e2dd888f",
"freqs": {
"52": 1,
"58": 1,
"92": 1,
"97": 1,
"99": 2
}
}
]
db.collection.aggregate([
{
$match: {}
},
{
$unwind: "$data"
},
{
$group: {
_id: "$data",
c: { $sum: 1 },
id: { $first: "$_id" }
}
},
{
$group: {
_id: "$id",
data: { $push: { k: "$_id", v: "$c" } }
}
},
{
$set: {
data: { $arrayToObject: "$data" }
}
}
])
mongoplayground
db.collection.aggregate([
{
$set: {
data: {
$function: {
body: "function(d) {let obj = {}; d.forEach(e => {if(obj[e]==null) { obj[e]=1; }else{ obj[e]++; }}); return obj;}",
args: [
"$data"
],
lang: "js"
}
}
}
}
])
mongoplayground
I have a collection that looks like below:
[
{
"orderNum": "100",
"createdTime": ISODate("2020-12-01T21:00:00.000Z"),
"amount": 100,
"memo": "100memo",
"list": [
1
]
},
{
"orderNum": "200",
"createdTime": ISODate("2020-12-01T21:01:00.000Z"),
"amount": 200,
"memo": "200memo",
"list": [
1,
2
]
},
{
"orderNum": "300",
"createdTime": ISODate("2020-12-01T21:02:00.000Z"),
"amount": 300,
"memo": "300memo"
},
{
"orderNum": "400",
"createdTime": ISODate("2020-12-01T21:03:00.000Z"),
"amount": 400,
"memo": "400memo"
},
]
and I'm trying to get the total amount of orders that were created before order# 300 (so order#100 and #200, total amount is 300).
Does anyone know how to get it via Mongoose?
You can use this one:
db.collection.aggregate([
{ $sort: { orderNum: 1 } }, // by default the order of documents in a collection is undetermined
{ $group: { _id: null, data: { $push: "$$ROOT" } } }, // put all documents into one document
{ $set: { data: { $slice: ["$data", { $indexOfArray: ["$data.orderNum", "300"] }] } } }, // cut desired elementes from array
{ $unwind: "$data" }, // transform back to documents
{ $replaceRoot: { newRoot: "$data" } },
{ $group: { _id: null, total_amount: { $sum: "$amount" } } } // make summary
])
Actually it is not needed to $unwind and $group, so the shortcut would be this:
db.collection.aggregate([
{ $sort: { orderNum: 1 } },
{ $group: { _id: null, data: { $push: "$$ROOT" } } },
{ $set: { data: { $slice: ["$data", { $indexOfArray: ["$data.orderNum", "300"] }] } } },
{ $project: { total_amount: { $sum: "$data.amount" } } }
])
But the answer from #turivishal is even better.
Update for additional field
{
$set: {
data: { $slice: ["$data", { $indexOfArray: ["$data.orderNum", "300"] }] },
memo: { $arrayElemAt: [ "$data.memo", { $indexOfArray: ["$data.orderNum", "300"] } ] }
}
}
or
{ $set: { data: { $slice: ["$data", { $indexOfArray: ["$data.orderNum", "300"] }] } } },
{ $set: { memo: { $last: { "$data.memo" } } },
$match orderNum less than 300
$group by null and get totalAmount using $sum of amount
YourSchemaModel.aggregate([
{ $match: { orderNum: { $lt: "300" } } },
{
$group: {
_id: null,
totalAmount: { $sum: "$amount" }
}
}
])
Playground
I have a MongoDB collection with records in the following format:
[
{ "item1": { "a": 1 }, "item2": { "a": 2 } },
{ "item1": { "a": 3 }, "item3": { "a": 4 } },
{ "item1": { "a": 5 }, "item2": { "a": 6 } },
]
I want to get a count of records having the fields item1, item2, and item3 (They don't need to be dynamic. I have only a finite set of items). What I need is a count of records with field existing in the following fashion:
{ "item1": 3, "item2": 2, "item3": 1 }
For getting the count for item1, I do this:
db.collection.find({ "item1": { $exists: true }}).count()
Is there an easy way to aggregate the count of all three items in a single query?
You can use $objectToArray and $arrayToObject to count your keys dynamically:
db.collection.aggregate([
{
$project: { root: { $objectToArray: "$$ROOT" } }
},
{
$unwind: "$root"
},
{
$group: { _id: "$root.k", total: { $sum: 1 } }
},
{
$group: { _id: null, obj: { $push: { k: "$_id", v: "$total" } } }
},
{
$replaceRoot: { newRoot: { $arrayToObject: "$obj" } }
},
{
$project: { _id: 0 }
}
])
Mongo Playground
I need help to count the elements of an array when it is in another array.
My command, when I tried to select the second array is,
db.artysci.aggregate([
{
$project: {
_id:0,
nazwa: 1,
nazwisko: 1,
numberOfSongs: { "album": {$size: "$utwor"}}
}
}
] )
Grid:
db.artysci.insert({
imie: 'Laurie',
nazwisko: 'Adkins',
rok_debiutu: 2006,
kraj_pochodzenia: ['Wielka Brytania'],
gatunek: 'neo soul',
album: [{
tytul:"19",
rok_edycji:2007,
gatunek: 'pop',
typ_nosnika: 'CD',
utwor: [{
numer: 1,
tytul_utworu: 'Daydreamer',
dlugosc_trwania: 3.41
},
{
numer: 2,
tytul_utworu: 'Best for Last',
dlugosc_trwania: 4.19
},
{
numer: 3,
tytul_utworu: 'Chasing Pavements',
dlugosc_trwania: 3.31
}
]
}]
})
Output when counting by $size:"$album",
{
"nazwisko" : "Adkins",
"numberOfSongs" : {
"album" : NumberInt(3)
}
}
How can I count elements of an array in an array by $size?
You can achieve this using Map and then summing it up. It works..
db.artysci.aggregate({
"$project": {
_id: 0,
nazwa: 1,
nazwisko: 1,
"numberOfAlbums": { "$size": { $ifNull: ["$album", []] } },
"numberOfSongs": {
"$sum": {
"$map": {
"input": "$album",
"in": { "$size": { $ifNull: ["$$this.utwor", []] } }
}
}
}
}
})
#Kacper,
Here is the soultion for your second question.
Yes, you can achieve it in either way, using the above method or using unwind and do the average..
Lets see an example using unwind:
Without divide/second:
db.notifications.aggregate([
{ $unwind: "$album" },
{ $unwind: "$album.utwor" },
{
$group: {
_id: "$_id",
avgDuration: { $avg: "$album.utwor.dlugosc_trwania" }
}
},
]);
With divide/second:
db.notifications.aggregate([
{ $unwind: "$album" },
{ $unwind: "$album.utwor" },
{
$group: {
_id: "$_id",
avgDuration: { $avg: { $divide: ["$album.utwor.dlugosc_trwania", 60] } }
}
},
]);
You can use $unwind and $group to get the counts.
db.collection.aggregate([
{
$unwind: "$album"
},
{
$unwind: "$album.utwor"
},
{
$group: {
_id: 0,
total: {
$sum: 1
}
}
}
])
Play
If you need more information, add it to your question.
With this MongoDB aggregation pipeline:
db.getCollection('device1_hour_events').aggregate([
{ $match: { 'ts_hour' : ISODate('2013-10-11T04:00:00.000Z') } },
{ $unwind: '$minutes' },
{ $match: { 'minutes.min': { $gt: -1, $lt: 2 } } },
{ $unwind: '$minutes.seconds' },
{ $group: { '_id': '$minutes.min',
'temp_min': { $min: '$minutes.seconds.temp' },
'temp_avg': { $avg: '$minutes.seconds.temp' },
'temp_max': { $max: '$minutes.seconds.temp' }
}
},
{ $sort: { '_id': 1} }
])
that produces the following result:
/* 1 */
{
"_id": 0,
"temp_min": 12,
"temp_avg": 47.25,
"temp_max": 99
}
/* 2 */
{
"_id": 1,
"temp_min": 35,
"temp_avg": 47.67,
"temp_max": 65
}
It's possible to obtain maybe with $project the following output:
{
"_id": [0, 1],
"temp_min": [12, 35],
"temp_avg": [47.25, 47.67],
"temp_max": [99, 65]
}
You can add another $group with a $push for each field :
{
$group: {
'_id': 0,
'_ids': { $push: '$_id' },
'temp_min': { $push: '$temp_min' },
'temp_avg': { $push: '$temp_avg' },
'temp_max': { $push: '$temp_max' }
}
}