Aggregate Field Values to Separate Key Names - mongodb

I have a collection in MongoDB with sample data something like this (simplified):
{
_id: 1,
username: "ted",
content: "4125151",
status: "complete"
}
{
_id: 2,
username: "sam",
content: "4151",
status: "new"
}
{
_id: 3,
username: "ted",
content: "511",
status: "new"
}
{
_id: 4,
username: "ted",
content: "411",
status: "in_progress"
}
{
_id: 5,
username: "pat",
content: "1sds51",
status: "complete"
}
{
_id: 6,
username: "ted",
content: "4151",
status: "in_progress"
}
{
_id: 7,
username: "ted",
content: "4125",
status: "in_progress"
}
I need to aggregate the data such that for each user, I get a count for each status value as well as a total number of records. The result should look like this:
[
{
username: “pat”,
new: 0,
in_progress: 0,
complete: 1,
total: 1
},
{
username: “sam”,
new: 1,
in_progress: 0,
complete: 0,
total: 1
},
{
username: “ted”,
new: 1,
in_progress: 3,
complete: 1,
total: 5
}
]
Or any format that will effectively serve the same purpose which is, I want to be able to use with ngRepeat to display on the front end in this format:
User New In Progress Complete Total
pat 0 0 1 1
sam 1 0 0 1
ted 1 3 1 5
I can perform this aggregation:
{
"$group": {
"_id": {
"username": "$username",
"status": "$status"
},
"count": {
"$sum": 1
}
}
}
This gives me the individual count for each user/status combination that has at least one record. But then I have to piece it together to get it in the format that I can use on the front end. This is not at all ideal.
Is there a way to perform the aggregation to get the data in the format that I need?

What you want is a "conditional" aggregation of the values to produce a distinct field property for each status.
This is pretty simple to do using the $cond operator:
[
{ "$group": {
"_id": "$username",
"new": { "$sum": { "$cond": [{ "$eq": [ "$status", "new" ] },1,0 ] } },
"complete": { "$sum": { "$cond": [{ "$eq": [ "$status", "complete" ] },1,0 ] } },
"in_progress": { "$sum": { "$cond": [{ "$eq": [ "$status", "in_progress" ] },1,0 ] } },
"total": { "$sum": 1 }
}}
]
Presuming of course those are the only "status" values, but if they are not then just add an additional $project to sum the fields you want:
[
{ "$match": { "status": { "$in": [ "new", "complete", "in_progress" ] } } },
{ "$group": {
"_id": "$username",
"new": { "$sum": { "$cond": [{ "$eq": [ "$status", "new" ] },1,0 ] } },
"complete": { "$sum": { "$cond": [{ "$eq": [ "$status", "complete" ] },1,0 ] } },
"in_progress": { "$sum": { "$cond": [{ "$eq": [ "$status", "in_progress" ] },1,0 ] } }
}},
{ "$project": {
"new": 1,
"complete": 1,
"in_progress": 1,
"total": { "$add": [ "$new", "$complete", "$in_progress" ] }
]
Or just include that $add within the $group with the same calculations for the separate fields. But the $match is probably just the best idea if there are indeed other status values you don't want.

Another answer using $group twice and a $push, In this below query you need to compute the final total on UI side.
db.collection.aggregate([
{
"$group": {
"_id": {
"username": "$username",
"status": "$status"
},
"statuscount": {
"$sum": 1
}
}
},
{
"$group": {
"_id": "$_id.username",
"finalstatus": {
"$push": {
"Status": "$_id.status",
"statuscount": "$statuscount"
}
}
}
}
])

Related

MongoDB group by and SUM by array

I'm new in mongoDB.
This is one example of record from collection:
{
supplier: 1,
type: "sale",
items: [
{
"_id": ObjectId("60ee82dd2131c5032342070f"),
"itemBuySum": 10
},
{
"_id": ObjectId("60ee82dd2131c50323420710"),
"itemBuySum": 10,
},
{
"_id": ObjectId("60ee82dd2131c50323420713"),
"itemBuySum": 10
},
{
"_id": ObjectId("60ee82dd2131c50323420714"),
"itemBuySum": 20
}
]
}
I need to group by TYPE field and get the SUM. This is output I need:
{
supplier: 1,
sales: 90,
returns: 170
}
please check Mongo playground for better understand. Thank you!
$match - Filter documents.
$group - Group by type and add item into data array which leads to the result like:
[
[/* data 1 */],
[/* data 2 */]
]
$project - Decorate output document.
3.1. First $reduce is used to flatten the nested array to a single array (from Result (2)) via $concatArrays.
3.2. Second $reduce is used to aggregate $sum the itemBuySum.
db.collection.aggregate({
$match: {
supplier: 1
},
},
{
"$group": {
"_id": "$type",
"supplier": {
$first: "$supplier"
},
"data": {
"$push": "$items"
}
}
},
{
"$project": {
_id: 0,
"supplier": "$supplier",
"type": "$_id",
"returns": {
"$reduce": {
"input": {
"$reduce": {
input: "$data",
initialValue: [],
in: {
"$concatArrays": [
"$$value",
"$$this"
]
}
}
},
"initialValue": 0,
"in": {
$sum: [
"$$value",
"$$this.itemBuySum"
]
}
}
}
}
})
Sample Mongo Playground
db.collection.aggregate([
{
$match: {
supplier: 1
},
},
{
"$group": {
"_id": "$ID",
"supplier": {
"$first": "$supplier"
},
"sale": {
"$sum": {
"$cond": {
"if": {
"$eq": [
"$type",
"sale"
]
},
"then": {
"$sum": "$items.itemBuySum"
},
"else": {
"$sum": 0
}
}
}
},
"returns": {
"$sum": {
"$sum": {
"$cond": {
"if": {
"$eq": [
"$type",
"return"
]
},
"then": {
"$sum": "$items.itemBuySum"
},
"else": {
"$sum": 0
}
}
}
}
}
}
},
{
"$project": {
_id: 0,
supplier: 1,
sale: 1,
returns: 1
}
}
])

MongoDB: Assign document objects to field in '$project' stage

I have a user collection:
[
{"_id": 1,"name": "John", "age": 25, "valid_user": true}
{"_id": 2, "name": "Bob", "age": 40, "valid_user": false}
{"_id": 3, "name": "Jacob","age": 27,"valid_user": null}
{"_id": 4, "name": "Amelia","age": 29,"valid_user": true}
]
I run a '$facet' stage on this collection. Checkout this MongoPlayground.
I want to talk about the first output from the facet stage. The following is the response currently:
{
"user_by_valid_status": [
{
"_id": false,
"count": 1
},
{
"_id": true,
"count": 2
},
{
"_id": null,
"count": 1
}
]
}
However, I want to restructure the output in this way:
"analytics": {
"invalid_user": {
"_id": false
"count": 1
},
"valid_user": {
"_id": true
"count": 2
},
"user_with_unknown_status": {
"_id": null
"count": 1
}
}
The problem with using a '$project' stage along with 'arrayElemAt' is that the order may not be definite for me to associate an index with an attribute like 'valid_users' or others. Also, it gets further complicated because unlike the sample documents that I have shared, my collection may not always contain all the three categories of users.
Is there some way I can do this?
You can use $switch conditional operator,
$project to show value part in v with _id and count field as object, k to put $switch condition
db.collection.aggregate([
{
"$facet": {
"user_by_valid_status": [
{
"$group": {
"_id": "$valid_user",
"count": { "$sum": 1 }
}
},
{
$project: {
_id: 0,
v: { _id: "$_id", count: "$count" },
k: {
$switch: {
branches: [
{ case: { $eq: ["$_id", null] }, then: "user_with_unknown_status" },
{ case: { $eq: ["$_id", false] }, then: "invalid_user" },
{ case: { $eq: ["$_id", true] }, then: "valid_user" }
]
}
}
}
}
],
"users_above_30": [{ "$match": { "age": { "$gt": 30 } } }]
}
},
$project stage in root, convert user_by_valid_status array to object using $arrayToObject
{
$project: {
analytics: { $arrayToObject: "$user_by_valid_status" },
users_above_30: 1
}
}
])
Playground

How to use conditional count while doing grouping in mongodb?

I need help in getting counts in the conditional grouping.
I have following JSON
[
{
"id": "103303dd56a731e377d01f6a37badae3",
"project_id": "10006",
"project_name": "Project_6",
"status": "TERM"
},
{
"id": "b63826f7edd2fc3ad8449add0c04fceb",
"project_id": "10004",
"project_name": "Project_4",
"status": "CMP"
},
{
"id": "d46e1fcf4c07ce4a69ee07e4134bcef1",
"project_id": "10008",
"project_name": "Project_8",
"status": "TERM"
},
{
"id": "a9fb9e6ef40426e9add520623d521ab8",
"project_id": "10001",
"project_name": "Project_1",
"status": "TERM"
},
{
"id": "b63826f7edd2fc3ad8449add0c04fceb",
"project_id": "10004",
"project_name": "Project_4",
"status": "QF"
}]
So you can see I have Duplicate Projects Records.
I want to get Data like this.
[
{
"project_id": "10007",
"starts": 2, //Count of records where project grouped
"Completes":3 //Where status="CMP"
"TERMS":6 //Where status="TERM"
"QFull":2 //Where status="QF",
"Abandons":3 //Where status=""
},
{
"project_id": "10004",
"starts": 3, //Count of records where project grouped
"Completes":2 //Where status="CMP"
"TERMS":4 //Where status="TERM"
"QFull":2 //Where status="QF",
"Abandons":1 //Where status=""
},
{
"project_id": "10001",
"starts": 3, //Count of records where project grouped
"Completes":2 //Where status="CMP"
"TERMS":4 //Where status="TERM"
"QFull":2 //Where status="QF",
"Abandons":1 //Where status=""
}
]
Here is the Fiddle for same: https://mongoplayground.net/p/yNerdPRjbxc
What I've tried so far:
db.collection.aggregate([
{
$group: {
_id: {
project_id: "$project_id"
},
project_id: {
$first: "$project_id"
},
starts: {
$sum: 1
}
}
}
])
I am not sure how can I add extra fields here based on conditions.
There is a $cond operator which can be used within $sum. So you simply add 1 if status matches your condition or 0 if it doesn't. You can try below aggregation:
db.col.aggregate([
{
$group: {
_id: "$project_id",
starts: { $sum: 1 },
Completes: { $sum: { $cond: [ { $eq: [ "$status", "CMP" ] }, 1, 0 ] } },
TERMS: { $sum: { $cond: [ { $eq: [ "$status", "TERM" ] }, 1, 0 ] } },
QFull: { $sum: { $cond: [ { $eq: [ "$status", "QF" ] }, 1, 0 ] } },
Abandons: { $sum: { $cond: [ { $eq: [ "$status", "" ] }, 1, 0 ] } },
}
},
{
$project: {
_id: 0,
project_id: "$_id",
starts: 1,
Completes: 1,
TERMS: 1,
QFull: 1,
Abandons: 1
}
}
])
Here is a Fiddle for the same: https://mongoplayground.net/p/JOZJOhyrnRL
this fiddle contains $match as well if you want to retrieve records for specific projects

Group books and get count for each of their ratings

I have a Rating model with a book and rating value to it. I would like to get all the ratings count (ratings vary from 1 to 5) for each book in the database.
My schema simply looks like -
{
"_id": ObjectId("57e112312a52fe257e5d1d5c"),
"book": ObjectId("57e111142a52fe257e5d1d42"),
"rating": 4
}
{
"_id": ObjectId("57e7a002420d22d6106a4715"),
"book": ObjectId("57e111142a52fe257e5d1d42"),
"rating": 5
}
{
"_id": ObjectId("57e7a4cd98bfdb5a11962d54"),
"book": ObjectId("57e111142a52fe257e5d17676"),
"rating": 5
}
{
"_id": ObjectId("57e7a4cd98bfdb5a11962d54"),
"book": ObjectId("57e111142a52fe257e5d17676"),
"rating": 1
}
Currently, i have only been able to get to this point where i can get the no of ratings for each book but it doesn't specify exactly the rating value count.
This is my current query -
db.ratings.aggregate([
{$match: {book: {$in: [ObjectId("57e111142a52fe257e5d1d42"), ObjectId('57e6bef7cad79fa38555c643')]}}},
{$group: {_id: {book: "$book", value: "$value"} } },
{$group: {_id: "$_id.book", total: {$sum: 1}}},
])
The output is this -
{
"result": [
{
"_id": ObjectId("57e6bef7cad79fa38555c643"),
"total": 2
},
{
"_id": ObjectId("57e111142a52fe257e5d1d42"),
"total": 2
}
],
"ok": 1
}
However, i want to club all the documents and get a result with the count of ratings for each value of the rating field, something like below. The whole point is that i just want the count of ratings for each value for each book.
{
result: [
{
_id: "57e111142a52fe257e5d17676",
5_star_ratings: 1,
4_star_ratings: 3,
3_star_ratings: 4,
2_star_ratings: 1,
1_star_ratings: 0,
},
{
_id: "57e111142a52fe257e5d1d42",
5_star_ratings: 10,
4_star_ratings: 13,
3_star_ratings: 7,
2_star_ratings: 8,
1_star_ratings: 19,
}
.
.
.
.
]
}
How do i go about this?
Accomplishing the task require a $group pipeline that uses the $cond operator in the $sum accumulator operator. The $cond operator will evaluate a logical condition based on its first argument (if) and then returns the second argument where the evaluation is true (then) or the third argument where false (else). This converts the true/false logic into 1 and 0 numerical values that feed into $sum respectively:
{
"$sum": {
"$cond": [ { "$eq": [ "$rating", 1 ] }, 1, 0 ]
}
}
As a resulting operation, you might want to run the following aggregation pipeline:
var pipeline = [
{
"$match": {
"book": {
"$in": [
ObjectId("57e111142a52fe257e5d1d42"),
ObjectId('57e6bef7cad79fa38555c643')
]
}
}
},
{
"$group": {
"_id": "$book",
"5_star_ratings": {
"$sum": {
"$cond": [ { "$eq": [ "$rating", 5 ] }, 1, 0 ]
}
},
"4_star_ratings": {
"$sum": {
"$cond": [ { "$eq": [ "$rating", 4 ] }, 1, 0 ]
}
},
"3_star_ratings": {
"$sum": {
"$cond": [ { "$eq": [ "$rating", 3 ] }, 1, 0 ]
}
},
"2_star_ratings": {
"$sum": {
"$cond": [ { "$eq": [ "$rating", 2 ] }, 1, 0 ]
}
},
"1_star_ratings": {
"$sum": {
"$cond": [ { "$eq": [ "$rating", 1 ] }, 1, 0 ]
}
}
}
},
]
db.ratings.aggregate(pipeline)
For a more flexible and better performant approach which executes much faster than the above, consider running an alternative pipeline as follows
db.ratings.aggregate([
{
"$match": {
"book": {
"$in": [
ObjectId("57e111142a52fe257e5d1d42"),
ObjectId('57e6bef7cad79fa38555c643')
]
}
}
},
{
"$group": {
"_id": {
"book": "$name",
"rating": "$rating"
},
"count": { "$sum": 1 }
}
},
{
"$group": {
"_id": "$_id.book",
"counts": {
"$push": {
"rating": "$_id.rating",
"count": "$count"
}
}
}
}
])

How to count fields in Mongodb Aggregation

I have a document with entries like this
{
"_id": ObjectId("5644c495d0807a1750043237"),
"siteid": "123456"
"amount": 1.32
}
Some documents have other amounts eg."cashbackAmount"
I want a sum and a count for each amount fields. Not every document contains all the amount fields.
I hjave tried the following
{
$group: {
"_id": "$siteid",
item2: { "$sum": "$amount" },
item3: { "$sum": "$totalAmount" },
item4: { "$sum": "$cashbackAmount" },
item5: { "$sum": "$unitPrice" },
}
}
It gives me the sum, but I cannot work out how to get the number times each amount field is present.
{ "$sum": 1 } does not work because that gives me all the documents that have any one of the totals fields.
I guess you probably want something like that
db.getCollection('amounts').aggregate([
{
$project: {
siteid: 1,
amount: 1,
totalAmount: 1,
unitPrice: 1,
cashbackAmount: 1,
amountPresent: {
$cond: {
if: "$amount",
then: 1,
else: 0
}
},
totalAmountPresent: {
$cond: {
if: "$totalAmount",
then: 1,
else: 0
}
},
cashbackAmountPresent: {
$cond: {
if: "$cashbackAmount",
then: 1,
else: 0
}
},
unitPricePresent: {
$cond: {
if: "$unitPrice",
then: 1,
else: 0
}
}
}
},
{
$group: {
"_id": "$siteid",
amountSum: { "$sum": "$amount" },
amountCount: { "$sum": "$amountPresent" },
totalAmountSum: { "$sum": "$totalAmount" },
totalAmountCount: { "$sum": "$totalAmountPresent" },
cashbackAmountSum: { "$sum": "$cashbackAmount" },
cashbackAmountCount: { "$sum": "$cashbackAmountPresent" },
unitPriceSum: { "$sum": "$unitPrice" },
unitPriceCount: { "$sum": "$unitPricePresent" }
}
}
])
If you know the amount fields in advance then you could do this in a single aggregation operation where you create the pipeline dynamically.
Check out the following demonstration:
var amountFields = ["amount", "totalAmount", "cashbackAmount", "unitPrice"],
groupOperator = { "$group": { "_id": "$siteid" } };
amountFields.forEach(function (field){
groupOperator["$group"][field+"Total"] = { "$sum": "$"+field };
groupOperator["$group"][field+"Count"] = {
"$sum": {
"$cond": [ { "$gt": [ "$"+field, null ] }, 1, 0 ]
}
};
});
db.test.aggregate([groupOperator]);
Populate Test Documents
db.test.insert([
{
"siteid": "123456",
"amount": 1.32
},
{
"siteid": "123456",
"cashbackAmount": 8.32
},
{
"siteid": "123456",
"cashbackAmount": 9.74
},
{
"siteid": "123456",
"unitPrice": 0.19
},
{
"siteid": "123456",
"amount": 27.8,
"totalAmount": 15.22,
"unitPrice": 5.10,
"cashbackAmount": 43.62
},
{
"siteid": "123456",
"unitPrice": 5.07
},
{
"siteid": "123456",
"amount": 12.98,
"totalAmount": 32.82
},
{
"siteid": "123456",
"amount": 6.65,
"unitPrice": 5.10
}
])
Sample Aggregation Output
{
"_id" : "123456",
"amountTotal" : 48.75,
"amountCount" : 4,
"totalAmountTotal" : 48.04,
"totalAmountCount" : 2,
"cashbackAmountTotal" : 61.68,
"cashbackAmountCount" : 3,
"unitPriceTotal" : 15.46,
"unitPriceCount" : 4
}