How to get count of multiple fields based on value in mongodb? - mongodb

Collection exists as below:
[
{"currentLocation": "Chennai", "baseLocation": "Bengaluru"},
{"currentLocation": "Chennai", "baseLocation": "Bengaluru"},
{"currentLocation": "Delhi", "baseLocation": "Bengaluru"},
{"currentLocation": "Chennai", "baseLocation": "Chennai"}
]
Expected Output:
[
{"city": "Chennai", "currentLocationCount": 3, "baseLocationCount": 1},
{"city": "Bengaluru", "currentLocationCount": 0, "baseLocationCount": 3},
{"city": "Delhi", "currentLocationCount": 1, "baseLocationCount": 0}
]
What I have tried is:
db.getCollection('users').aggregate([{
$group: {
"_id": "$baselocation",
baseLocationCount: {
$sum: 1
}
},
}, {
$project: {
"_id": 0,
"city": "$_id",
"baseLocationCount": 1
}
}])
Got result as:
[
{"city": "Chennai", "baseLocationCount": 1},
{"city": "Bengaluru", "baseLocationCount": "3"}
]
I'm not familiar with mongo, so any help?
MongoDB Version - 3.4

Neil Lunn and myself had a lovely argument over this topic the other day which you can read all about here: Group by day with Multiple Date Fields.
Here are two solutions to your precise problem.
The first one uses the $facet stage. Bear in mind, though, that it may not be suitable for large collections because $facet produces a single (potentially huge) document that might be bigger than the current MongoDB document size limit of 16MB (which only applies to the result document and wouldn't be a problem during pipeline processing anyway):
collection.aggregate(
{
$facet:
{
"current":
[
{
$group:
{
"_id": "$currentLocation",
"currentLocationCount": { $sum: 1 }
}
}
],
"base":
[
{
$group:
{
"_id": "$baseLocation",
"baseLocationCount": { $sum: 1 }
}
}
]
}
},
{ $project: { "result": { $setUnion: [ "$current", "$base" ] } } }, // merge results into new array
{ $unwind: "$result" }, // unwind array into individual documents
{ $replaceRoot: { newRoot: "$result" } }, // get rid of the additional field level
{ $group: { "_id": "$_id", "currentLocationCount": { $sum: "$currentLocationCount" }, "baseLocationCount": { $sum: "$baseLocationCount" } } }, // group into final result)
{ $project: { "_id": 0, "city": "$_id", "currentLocationCount": 1, "baseLocationCount": 1 } } // group into final result
)
The second one works based on the $map stage instead:
collection.aggregate(
{
"$project": {
"city": {
"$map": {
"input": [ "current", "base" ],
"as": "type",
"in": {
"type": "$$type",
"name": {
"$cond": {
"if": { "$eq": [ "$$type", "current" ] },
"then": "$currentLocation",
"else": "$baseLocation"
}
}
}
}
}
}
},
{ "$unwind": "$city" },
{
"$group": {
"_id": "$city.name",
"currentLocationCount": {
"$sum": {
"$cond": {
"if": { "$eq": [ "$city.type", "current" ] },
"then": 1,
"else": 0
}
}
},
"baseLocationCount": {
"$sum": {
"$cond": {
"if": { "$eq": [ "$city.type", "base" ] },
"then": 1,
"else": 0
}
}
}
}
}
)

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
}
}
])

How to add a summary header/footer of a dataset within the same query using Mongodb

Let the following dataset (_id ommited for clarity sakes)
{ "model":"Nissan", "regId": 1230, "status": "active", "regCost" :100},
{ "model":"Nissan", "regId": 1231, "status": "active", "regCost" :100 },
{ "model":"Nissan", "regId": 1232, "status": "inactive", "regCost" :0},
{ "model":"Honda", "regId": 1233, "status": "active", "regCost" :90},
{ "model":"Honda", "regId": 1234, "status": "active", "regCost" :90},
{ "model":"Toyota", "regId": 1235, "status": "active", "regCost" :80}
Running the following query in Mongo
[
{
"$group": {
"_id": "$model",
"TotalActive": {
"$sum": {
"$cond": {
"if": {
"$eq": ["$status", "active"]
},
"then": 1,
"else": 0
}
}
},
"TotalCost" : {"$sum" : "$regCost"}
}
}
]
will give this above result:
The question is how can I modify my query in order to add a summary row like:
You can use below aggregation
db.collection.aggregate([
{ "$group": {
"_id": "$model",
"TotalActive": {
"$sum": {
"$cond": {
"if": {
"$eq": ["$status", "active"]
},
"then": 1,
"else": 0
}
}
},
"TotalCost": { "$sum": "$regCost" }
}},
{ "$facet": {
"total": [
{ "$group": {
"_id": "Total",
"TotalActive": { "$sum": "$TotalActive" },
"TotalCost": { "$sum": "$TotalCost" }
}}
],
"data": [{ "$match": {} }]
}},
{ "$project": {
"data": {
"$concatArrays": ["$data", "$total"]
}
}},
{ "$unwind": "$data" },
{ "$replaceRoot": { "newRoot": "$data" } }
])
MongoPlayground

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"
}
}
}
}
])

mongodb aggregation for calculating attendance

I have a collection that has a document with the following value
{
"_id": ObjectID("XXXXXX"),
"more details" :more details
"attendance": [
{
"date": ISODate("2015-08-11T18:30:00.000Z"),
"students": [
{
"studentId": ObjectID("YYYYYY"),
"entryTime": ISODate("1970-01-01T05:41:00.000Z"),
"exitTime": ISODate("1970-01-01T05:41:00.000Z"),
"attendanceStatus": "Present"
},
{
"studentId": ObjectID("ZZZZZZ"),
"entryTime": ISODate("1970-01-01T06:42:00.000Z"),
"exitTime": ISODate("1970-01-01T06:42:00.000Z"),
"attendanceStatus": "Present"
}
]
},
{
"date": ISODate("2015-08-12T18:30:00.000Z"),
"students": [
{
"studentId": ObjectID("XXXXX"),
"entryTime": ISODate("1970-01-01T05:41:00.000Z"),
"exitTime": ISODate("1970-01-01T06:42:00.000Z"),
"attendanceStatus": "Present"
},
{
"studentId": ObjectID("YYYYY"),
"entryTime": ISODate("1970-01-01T05:41:00.000Z"),
"exitTime": ISODate("1970-01-01T06:42:00.000Z"),
"attendanceStatus": "Absent"
}
]
}
]
}
I am trying to get the number of days a student has been present and the number of days that he has been absent. But i keep getting the output as undefined.
var pipeline = [
{
"$match": {_id: mongoose.Types.ObjectId(batchId)}
},
{
"$project": {attendance: '$attendance.students'}
},
{
"$group": {
"studentId": "$studentId"
,
"Present": {
"$sum": {
"$cond": [{"$eq": ["attendanceStatus", "Present"]}, 1, 0]
}
},
"Absent": {
"$sum": {
"$cond": [{"$eq": ["attendanceStatus", "Absent"]}, 1, 0]
}
}
}
}
];
Am unable to figure out what I am missing out on
Please try this pipeline
var pipeline = [{$unwind: '$attendance'},
{$unwind: '$attendance.students'},
{$group: {
_id: '$attendance.students.studentId',
"Present": {
"$sum": {
"$cond": [
{"$eq": ["$attendance.students.attendanceStatus", "Present"]},
1,
0]
}
},
"Absent": {
"$sum": {
"$cond": [
{"$eq": ["$attendance.students.attendanceStatus", "Absent"]},
1,
0]
}
}}}]

Mongo Aggregation : $group and $project array to object for counts

I have documents like:
{
"platform":"android",
"install_date":20151029
}
platform - can have one value from [android|ios|kindle|facebook ] .
install_date - there are many install_dates
There are also many fields.
Aim : I am calculating installs per platform on particular date.
So I am using group by in aggregation framework and make counts by platform. Document should look like like:
{
"install_date":20151029,
"platform" : {
"android":1000,
"ios": 2000,
"facebook":1500
}
}
I have done like:
db.collection.aggregate([
{
$group: {
_id: { platform: "$platform",install_date:"$install_date"},
count: { "$sum": 1 }
}
},
{
$group: {
_id: { install_date:"$_id.install_date"},
platform: { $push : {platform :"$_id.platform", count:"$count" } }
}
},
{
$project : { _id: 0, install_date: "$_id.install_date", platform: 1 }
}
])
which Gives document like:
{
"platform": [
{
"platform": "facebook",
"count": 1500
},
{
"platform": "ios",
"count": 2000
},
{
"platform": "android",
"count": 1000
}
],
"install_date": 20151027
}
Problem:
Projecting array to single object as "platform"
With MongoDb 3.4 and newer, you can leverage the use of $arrayToObject operator to get the desired result. You would need to run the following aggregate pipeline:
db.collection.aggregate([
{ "$group": {
"_id": {
"date": "$install_date",
"platform": { "$toLower": "$platform" }
},
"count": { "$sum": 1 }
} },
{ "$group": {
"_id": "$_id.date",
"counts": {
"$push": {
"k": "$_id.platform",
"v": "$count"
}
}
} },
{ "$addFields": {
"install_date": "$_id",
"platform": { "$arrayToObject": "$counts" }
} },
{ "$project": { "counts": 0, "_id": 0 } }
])
For older versions, take advantage of the $cond operator in the $group pipeline step to evaluate the counts based on the platform field value, something like the following:
db.collection.aggregate([
{ "$group": {
"_id": "$install_date",
"android_count": {
"$sum": {
"$cond": [ { "$eq": [ "$platform", "android" ] }, 1, 0 ]
}
},
"ios_count": {
"$sum": {
"$cond": [ { "$eq": [ "$platform", "ios" ] }, 1, 0 ]
}
},
"facebook_count": {
"$sum": {
"$cond": [ { "$eq": [ "$platform", "facebook" ] }, 1, 0 ]
}
},
"kindle_count": {
"$sum": {
"$cond": [ { "$eq": [ "$platform", "kindle" ] }, 1, 0 ]
}
}
} },
{ "$project": {
"_id": 0, "install_date": "$_id",
"platform": {
"android": "$android_count",
"ios": "$ios_count",
"facebook": "$facebook_count",
"kindle": "$kindle_count"
}
} }
])
In the above, $cond takes a logical condition as it's first argument (if) and then returns the second argument where the evaluation is true (then) or the third argument where false (else). This makes true/false returns into 1 and 0 to feed to $sum respectively.
So for example, if { "$eq": [ "$platform", "facebook" ] }, is true then the expression will evaluate to { $sum: 1 } else it will be { $sum: 0 }