My Document Structure(Only 2 given just for the idea):
/* 1 */
{
"_id" : ObjectId("59edc58af33e9b5988b875fa"),
"Agent" : {
"Name" : "NomanAgent",
"Location" : "Lahore",
"AgentId" : 66,
"Suggestion" : [
"Knowledge",
"Professionalisn"
]
},
"Rating" : 2,
"Status" : "Submitted"
}
/* 2 */
{
"_id" : ObjectId("59edc58af33e9b5988b875fb"),
"Agent" : {
"Name" : "NomanAgent",
"Location" : "Lahore",
"AgentId" : 66,
"Suggestion" : [
"Knowledge",
"Clarity"
]
},
"Rating" : 1,
"Status" : "Submitted"
}
/* 3 */
{
"_id" : ObjectId("59edc58af33e9b5988b875fc"),
"Agent" : {
"Name" : "NomanAgent",
"Location" : "Lahore",
"AgentId" : 66,
"Reward" : "Thumb Up"
},
"Rating" : 5,
"Status" : "Submitted"
}
These are basically the survey responses, so an Agent object could contain either a Suggestion(in case of bad customer review) or a Reward(in case of a happy customer) so here I am showing 2 documents with Suggestions and 1 with Reward.
I have created a query for the Rewards which is given below,
db.getCollection('_survey.response').aggregate([
{
$group:{
_id: "$Agent.Name",
Rating: {$avg: "$Rating"},
Rewards: {$push: "$Agent.Reward"},
Status: {$push : "$Status"}
}
},
{
$unwind: "$Rewards"
},
{
$group:{
_id: {
Agent: "$_id",
Rating: "$Rating",
Rewards: "$Rewards"
},
RewardCount:{$sum: 1},
SurveyStatus: {$first: "$Status"}
}
},
{
$group:{
_id: "$_id.Agent",
Rewards: {$push:{Reward: "$_id.Rewards", Count: "$RewardCount"}},
Rating: {$first: "$_id.Rating"},
SurveyStatus: {$first: "$SurveyStatus"}
}
},
{
$unwind: "$SurveyStatus"
},
{
$group:{
_id: {
Agent: "$_id",
Survey: "$SurveyStatus"
},
StatusCount:{$sum : 1},
Rating: {$first: "$Rating"},
Rewards: {$first: "$Rewards"}
}
},
{
$group:{
_id: "$_id.Agent",
Status:{$push:{Status: "$_id.Survey", Count: "$StatusCount"}},
Rewards: {$first: "$Rewards"},
Rating: {$first: "$Rating"}
}
},
{
$project:{
_id: 0,
Agent: "$_id",
Rating: {
$multiply:[
{$divide:["$Rating",5]},
100
]
},
Status: 1,
Rewards: 1
}
}
]);
Above query works perfectly fine for the rewards, i want exactly the same thing for suggestions and i would be happy if its possible to adjust Suggestions in the same query(We can also create a separate query for suggestion).
Response of above given query:
/* 1 */
{
"Status" : [
{
"Status" : "Submitted",
"Count" : 2.0
},
{
"Status" : "Pending",
"Count" : 1.0
},
{
"Status" : "Opened",
"Count" : 2.0
}
],
"Rewards" : [
{
"Reward" : "Thumb Up",
"Count" : 1.0
},
{
"Reward" : "Thank You",
"Count" : 2.0
}
],
"Agent" : "GhazanferAgent",
"Rating" : 68.0
}
/* 2 */
{
"Status" : [
{
"Status" : "Opened",
"Count" : 2.0
},
{
"Status" : "Viewed",
"Count" : 2.0
},
{
"Status" : "Pending",
"Count" : 3.0
}
],
"Rewards" : [
{
"Reward" : "Gift",
"Count" : 1.0
},
{
"Reward" : "Thumb Up",
"Count" : 3.0
},
{
"Reward" : "Thank You",
"Count" : 1.0
}
],
"Agent" : "NomanAgent",
"Rating" : 60.0
}
What I have tried so far, I think of two approaches but find an issue with each of them,
First(Find avg rating and push status and suggestions in array):
db.getCollection("_survey.response").aggregate([
{
$match:
{
$and:[
{
"Agent.Suggestion":{
$exists: true
}
},
{
Rating: {$lte: 3}
}
]
}
},
{
$group:{
_id: {
AgentName: "$Agent.Name",
AgentId: "$Agent.AgentId",
Location: "$Agent.Location"
},
Rating: {$avg: "$Rating"},
Status: {$push : "$Status"},
Suggestions: {$push: "$Agent.Suggestion"}
}
}
]);
Issue facing with this approach is, suggestions in the projection will become an array of arrays(as it was initially an array) of dynamic size depending on the number of times an agent gets a suggestion in a customer response. So the problem is applying $unwind on 2D array of dynamic size.
Second($unwind the suggestions in the first stage as its a 1D array
to avoid $unwind issue on 2D array of dynamic size)
db.getCollection("_survey.response").aggregate([
{
$match:
{
$and:[
{
"Agent.Suggestion":{
$exists: true
}
},
{
Rating: {$lte: 3}
}
]
}
},
{
$unwind: "$Agent.Suggestion"
},
{
$group: {
_id:{
AgentName: "$Agent.Name",
AgentId: "$Agent.AgentId",
Suggestion: "$Agent.Suggestion",
Location: "$Agent.Location"
},
Status: {$push: "$Status"},
Rating: {$avg: "$Rating"},
Count: {$sum: 1}
}
}
]);
Problem using this approach is $unwind Suggestion array it will flatten all suggestion with their respective agents thus increasing the number of documents(as compared to original responses) so i won't be able to find correct value for average rating for each agent on the basis of this grouping and the same will happen the Status(Because i can correctly find these two fields only if i group by agent. While, here i am grouping with agent along with suggestion),
I want exactly the same response for Suggestion query, only the Rewards object in response would replace Suggestions(Or it would great if we could get Suggestions object in the same response)
Survey Status can be, pending, Opened,viewed, Submitted etc
Output explanation:
I want suggestions(with counts), status(with counts) and Rating in % form(which i am already doing) for each of the agent as you can see in the output mentioned above.
Thanks in advance!!
Using $unwind two consecutive times did the trick for me, using First approach,
db.getCollection("_survey.response").aggregate([
{
$match:
{
$and:[
{
"Agent.Suggestion":{
$exists: true
}
},
{
Rating: {$lte: 3}
}
]
}
},
{
$group:{
_id: {
AgentName: "$Agent.Name",
AgentId: "$Agent.AgentId",
Location: "$Agent.Location"
},
Rating: {$avg: "$Rating"},
Status: {$push : "$Status"},
Suggestions: {$push: "$Agent.Suggestion"}
}
},
{
$unwind: "$Suggestions"
},
{
$unwind: "$Suggestions"
},
{
$group: {
_id: {
Suggestions: "$Suggestions",
AgentName: "$_id.AgentName",
AgentId: "$_id.AgentId",
Location: "$_id.Location"
},
SuggestionCount: {$sum: 1},
Rating: {$first: "$Rating"},
Status: {$first: "$Status"}
}
},
{
$group: {
_id:{
AgentName: "$_id.AgentName",
AgentId: "$_id.AgentId",
Location: "$_id.Location"
},
Suggestions: {$push:{Sugestion: "$_id.Suggestions", Count: "$SuggestionCount"}},
TotalSuggestions: {$sum: "$SuggestionCount"},
Rating: {$first: "$Rating"},
Status: {$first: "$Status"}
}
},
{
$unwind: "$Status"
},
{
$group:{
_id: {
AgentName: "$_id.AgentName",
AgentId: "$_id.AgentId",
Location: "$_id.Location",
Status: "$Status"
},
StatusCount:{$sum : 1},
Rating: {$first: "$Rating"},
Suggestions: {$first: "$Suggestions"},
TotalSuggestions: {$first: "$TotalSuggestions"}
}
},
{
$group:{
_id: {
AgentName: "$_id.AgentName",
AgentId: "$_id.AgentId",
Location: "$_id.Location"
},
Status:{$push:{Status: "$_id.Status", Count: "$StatusCount"}},
TotalStatus: {$sum: "$StatusCount"},
Suggestions: {$first: "$Suggestions"},
TotalSuggestions: {$first: "$TotalSuggestions"},
Rating: {$first: "$Rating"}
}
},
{
$project: {
_id: 0,
AgentName: "$_id.AgentName",
AgentId: "$_id.AgentId",
Location: "$_id.Location",
Status: 1,
TotalStatus: 1,
Suggestions: 1,
TotalSuggestions: 1,
Performance: {
$concat: [
{
$substr: [
{
$multiply:[
{$divide:["$Rating",5]},
100
]
}, 0, 4
]
},"%"
]
}
}
}
]);
Related
I have a collection of 1000 documents like this:
{
"_id" : ObjectId("628b63d66a5951db6bb79905"),
"index" : 0,
"name" : "Aurelia Gonzales",
"isActive" : false,
"registered" : ISODate("2015-02-11T04:22:39.000+0000"),
"age" : 41,
"gender" : "female",
"eyeColor" : "green",
"favoriteFruit" : "banana",
"company" : {
"title" : "YURTURE",
"email" : "aureliagonzales#yurture.com",
"phone" : "+1 (940) 501-3963",
"location" : {
"country" : "USA",
"address" : "694 Hewes Street"
}
},
"tags" : [
"enim",
"id",
"velit",
"ad",
"consequat"
]
}
I want to group those by year and gender. Like In 2014 male registration 105 and female registration 131. And finally return documents like this:
{
_id:2014,
male:105,
female:131,
total:236
},
{
_id:2015,
male:136,
female:128,
total:264
}
I have tried till group by registered and gender like this:
db.persons.aggregate([
{ $group: { _id: { year: { $year: "$registered" }, gender: "$gender" }, total: { $sum: NumberInt(1) } } },
{ $sort: { "_id.year": 1,"_id.gender":1 } }
])
which is return document like this:
{
"_id" : {
"year" : 2014,
"gender" : "female"
},
"total" : 131
}
{
"_id" : {
"year" : 2014,
"gender" : "male"
},
"total" : 105
}
Please guide to figure out from this whole.
db.collection.aggregate([
{
"$group": { //Group things
"_id": "$_id.year",
"gender": {
"$addToSet": {
k: "$_id.gender",
v: "$total"
}
},
sum: { //Sum it
$sum: "$total"
}
}
},
{
"$project": {//Reshape it
g: {
"$arrayToObject": "$gender"
},
_id: 1,
sum: 1
}
},
{
"$project": { //Reshape it
_id: 1,
"g.female": 1,
"g.male": 1,
sum: 1
}
}
])
Play
Just add one more group stage to your aggregation pipeline, like this:
db.persons.aggregate([
{ $group: { _id: { year: { $year: "$registered" }, gender: "$gender" }, total: { $sum: NumberInt(1) } } },
{ $sort: { "_id.year": 1,"_id.gender":1 } },
{
$group: {
_id: "$_id.year",
male: {
$sum: {
$cond: {
if: {
$eq: [
"$_id.gender",
"male"
]
},
then: "$total",
else: 0
}
}
},
female: {
$sum: {
$cond: {
if: {
$eq: [
"$_id.gender",
"female"
]
},
then: "$total",
else: 0
}
}
},
total: {
$sum: "$total"
}
},
}
]);
Here's the working link. We are grouping by year in this last step, and calculating the counts for gender conditionally and the total is just the total of the counts irrespective of the gender.
Besides #Gibbs mentioned in the comment which proposes the solution with 2 $group stages,
You can achieve the result as below:
$group - Group by year of registered. Add gender value into genders array.
$sort - Order by _id.
$project - Decorate output documents.
3.1. male - Get the size of array from $filter the value of "male" in "genders" array.
3.2. female - Get the size of array from $filter the value of "female" in "genders" array.
3.3. total - Get the size of "genders" array.
Propose this method if you are expected to count and return the "male" and "female" gender fields.
db.collection.aggregate([
{
$group: {
_id: {
$year: "$registered"
},
genders: {
$push: "$gender"
}
}
},
{
$sort: {
"_id": 1
}
},
{
$project: {
_id: 1,
male: {
$size: {
$filter: {
input: "$genders",
cond: {
$eq: [
"$$this",
"male"
]
}
}
}
},
female: {
$size: {
$filter: {
input: "$genders",
cond: {
$eq: [
"$$this",
"female"
]
}
}
}
},
total: {
$size: "$genders"
}
}
}
])
Sample Mongo Playground
I have a dataset in mongodb collection named visitorsSession like
{ip : 192.2.1.1,country : 'US', type : 'Visitors',date : '2019-12-15T00:00:00.359Z'},
{ip : 192.3.1.8,country : 'UK', type : 'Visitors',date : '2019-12-15T00:00:00.359Z'},
{ip : 192.5.1.4,country : 'UK', type : 'Visitors',date : '2019-12-15T00:00:00.359Z'},
{ip : 192.8.1.7,country : 'US', type : 'Visitors',date : '2019-12-15T00:00:00.359Z'},
{ip : 192.1.1.3,country : 'US', type : 'Visitors',date : '2019-12-15T00:00:00.359Z'}
I am using this mongodb aggregation
[{$match: {
nsp : "/hrm.sbtjapan.com",
creationDate : {
$gte: "2019-12-15T00:00:00.359Z",
$lte: "2019-12-20T23:00:00.359Z"
},
type : "Visitors"
}}, {$group: {
_id : "$country",
totalSessions : {
$sum: 1
}
}}, {$project: {
_id : 0,
country : "$_id",
totalSessions : 1
}}, {$sort: {
country: -1
}}]
using above aggregation i am getting results like this
[{country : 'US',totalSessions : 3},{country : 'UK',totalSessions : 2}]
But i also total visitors also along with result like totalVisitors : 5
How can i do this in mongodb aggregation ?
You can use $facet aggregation stage to calculate total visitors as well as visitors by country in a single pass:
db.visitorsSession.aggregate( [
{
$match: {
nsp : "/hrm.sbtjapan.com",
creationDate : {
$gte: "2019-12-15T00:00:00.359Z",
$lte: "2019-12-20T23:00:00.359Z"
},
type : "Visitors"
}
},
{
$facet: {
totalVisitors: [
{
$count: "count"
}
],
countrySessions: [
{
$group: {
_id : "$country",
sessions : { $sum: 1 }
}
},
{
$project: {
country: "$_id",
_id: 0,
sessions: 1
}
}
],
}
},
{
$addFields: {
totalVisitors: { $arrayElemAt: [ "$totalVisitors.count" , 0 ] },
}
}
] )
The output:
{
"totalVisitors" : 5,
"countrySessions" : [
{
"sessions" : 2,
"country" : "UK"
},
{
"sessions" : 3,
"country" : "US"
}
]
}
You could be better off with two queries to do this.
To save the two db round trips following aggregation can be used which IMO is kinda verbose (and might be little expensive if documents are very large) to just count the documents.
Idea: Is to have a $group at the top to count documents and preserve the original documents using $push and $$ROOT. And then before other matches/filter ops $unwind the created array of original docs.
db.collection.aggregate([
{
$group: {
_id: null,
docsCount: {
$sum: 1
},
originals: {
$push: "$$ROOT"
}
}
},
{
$unwind: "$originals"
},
{ $match: "..." }, //and other stages on `originals` which contains the source documents
{
$group: {
_id: "$originals.country",
totalSessions: {
$sum: 1
},
totalVisitors: {
$first: "$docsCount"
}
}
}
]);
Sample O/P: Playground Link
[
{
"_id": "UK",
"totalSessions": 2,
"totalVisitors": 5
},
{
"_id": "US",
"totalSessions": 3,
"totalVisitors": 5
}
]
I've got some event data captured in a MongoDB database, and some of these events occur in pairs.
Eg: DOOR_OPEN and DOOR_CLOSE are two events that occur in pairs
Events collection:
{ _id: 1, name: "DOOR_OPEN", userID: "user1", timestamp: t }
{ _id: 2, name: "DOOR_OPEN", userID: "user2", timestamp: t+5 }
{ _id: 3, name: "DOOR_CLOSE", userID: "user1", timestamp:t+10 }
{ _id: 4, name: "DOOR_OPEN", userID: "user1", timestamp:t+30 }
{ _id: 5, name: "SOME_OTHER_EVENT", userID: "user3", timestamp:t+35 }
{ _id: 6, name: "DOOR_CLOSE", userID: "user2", timestamp:t+40 }
...
Assuming the records are sorted on the timestamp, the _id: 1 and _id: 3 are a "pair" for "user1. _id: 2 and _id: 6 for "user2".
I'd like to take all these DOOR_OPEN & DOOR_CLOSE pairs per user and calculate the average duration etc. the door has been opened by each user.
Can this be achieved using the aggregate framework?
You can use $lookup and $group for achieving this.
db.getCollection('TestColl').aggregate([
{ $match: {"name": { $in: [ "DOOR_OPEN", "DOOR_CLOSE" ] } }},
{ $lookup:
{
from: "TestColl",
let: { userID_lu: "$userID", name_lu: "$name", timestamp_lu :"$timestamp" },
pipeline: [
{ $match:
{ $expr:
{ $and:
[
{ $eq: [ "$userID", "$$userID_lu" ] },
{ $eq: [ "$$name_lu", "DOOR_OPEN" ]},
{ $eq: [ "$name", "DOOR_CLOSE" ]},
{ $gt: [ "$timestamp", "$$timestamp_lu" ] }
]
}
}
},
],
as: "close_dates"
}
},
{ $addFields: { "close_time": { $arrayElemAt: [ "$close_dates.timestamp", 0 ] } } },
{ $addFields: { "time_diff": { $divide: [ { $subtract: [ "$close_time", "$timestamp" ] }, 1000 * 60 ]} } }, // Minutes
{ $group: { _id: "$userID" ,
events: { $push: { "eventId": "$_id", "name": "$name", "timestamp": "$timestamp" } },
averageTimestamp: {$avg: "$time_diff"}
}
}
])
Sample Data:
[
{ _id: 1, name: "DOOR_OPEN", userID: "user1", timestamp: ISODate("2019-10-24T08:00:00Z") },
{ _id: 2, name: "DOOR_OPEN", userID: "user2", timestamp: ISODate("2019-10-24T08:05:00Z") },
{ _id: 3, name: "DOOR_CLOSE", userID: "user1", timestamp:ISODate("2019-10-24T08:10:00Z") },
{ _id: 4, name: "DOOR_OPEN", userID: "user1", timestamp:ISODate("2019-10-24T08:30:00Z") },
{ _id: 5, name: "SOME_OTHER_EVENT", userID: "user3", timestamp:ISODate("2019-10-24T08:35:00Z") },
{ _id: 6, name: "DOOR_CLOSE", userID: "user2", timestamp:ISODate("2019-10-24T08:40:00Z") },
{ _id: 7, name: "DOOR_CLOSE", userID: "user1", timestamp:ISODate("2019-10-24T08:50:00Z") },
{ _id: 8, name: "DOOR_OPEN", userID: "user2", timestamp:ISODate("2019-10-24T08:55:00Z") }
]
Result:
/* 1 */
{
"_id" : "user2",
"events" : [
{
"eventId" : 2.0,
"name" : "DOOR_OPEN",
"timestamp" : ISODate("2019-10-24T08:05:00.000Z")
},
{
"eventId" : 6.0,
"name" : "DOOR_CLOSE",
"timestamp" : ISODate("2019-10-24T08:40:00.000Z")
},
{
"eventId" : 8.0,
"name" : "DOOR_OPEN",
"timestamp" : ISODate("2019-10-24T08:55:00.000Z")
}
],
"averageTimestamp" : 35.0
}
/* 2 */
{
"_id" : "user1",
"events" : [
{
"eventId" : 1.0,
"name" : "DOOR_OPEN",
"timestamp" : ISODate("2019-10-24T08:00:00.000Z")
},
{
"eventId" : 3.0,
"name" : "DOOR_CLOSE",
"timestamp" : ISODate("2019-10-24T08:10:00.000Z")
},
{
"eventId" : 4.0,
"name" : "DOOR_OPEN",
"timestamp" : ISODate("2019-10-24T08:30:00.000Z")
},
{
"eventId" : 7.0,
"name" : "DOOR_CLOSE",
"timestamp" : ISODate("2019-10-24T08:50:00.000Z")
}
],
"averageTimestamp" : 15.0
}
You could use the $group operator of the aggregate framework to group by userID and calculate the averages:
db.events.aggregate([{
$group: {
_id: "$userID",
averageTimestamp: {$avg: "$timestamp"}
}
}]);
If you also want to discard any other event other than DOOR_OPEN or DOOR_CLOSED, you can add a filter adding a $match in the aggregate pipeline:
db.events.aggregate([{
$match: {
$or: [{name: "DOOR_OPEN"},{name: "DOOR_CLOSE"}]
}
}, {
$group: {
_id: "$userID",
averageTimestamp: {$avg: "$timestamp"}
}
}]);
I want to get data to each month. in my table data is stored like this:-
"patient" : [
{
"status" : 'arrived',
start_time: '2017-08-17T09:17:00.000Z
},
{
"status" : 'arraived',
start_time: '2017-08-16T07:17:00.000Z
},
{
"status" : 'arrived',
start_time: '2017-07-12T09:17:00.000Z
},
{
"status" : 'arraived',
start_time: '2017-07-05T08:10:00.000Z
},
{
"status" : 'arrived',
start_time: '2017-06-02T09:17:00.000Z
},
{
"status" : 'arraived',
start_time: '2017-05-05T08:16:00.000Z
}
]
etc,
and I want to sum of patient of each month (jan to des), like this :-
{
"month" : 8,
"count" : 2
}and like this month 1 to 12
I assume, patient array is associated with a customer and the date is stored in mongo ISO format.
So, the actual document would look like :
{
name: "stackOverflow",
"patient" : [
{
"status" : 'arrived',
"start_time": ISODate("2017-08-17T09:17:00.000Z")
},
{
"status" : 'arraived',
"start_time": ISODate("2017-08-16T07:17:00.000Z")
},
{
"status" : 'arrived',
"start_time": ISODate("2017-07-12T09:17:00.000Z")
},
{
"status" : 'arraived',
"start_time": ISODate("2017-07-05T08:10:00.000Z")
},
{
"status" : 'arrived',
"start_time": ISODate("2017-06-02T09:17:00.000Z")
},
{
"status" : 'arraived',
"start_time": ISODate("2017-05-05T08:16:00.000Z")
}
]
}
here is a sample query which you can try -
db.test.aggregate([
{$unwind: "$patient"},
{ $group: {
_id: {name: "$name", month: {$month: "$patient.start_time"}},
count: { $sum: 1}
}},
{$group: {
_id: "$_id.name",
patient: {$push: {month: "$_id.month", count: "$count"}}
}}
])
Sample output:
{
"_id" : "stackOverflow",
"patient" : [
{
"month" : 5,
"count" : 1
},
{
"month" : 6,
"count" : 1
},
{
"month" : 7,
"count" : 2
},
{
"month" : 8,
"count" : 2
}
]
}
You can change query according to your use-case. hope this will help you!
This is my code:-
db.appointments.aggregate( [
{
$project:
{
"patient_id": 1,
"start_time": 1,
"status": 1
}
},
{
$match: {
'start_time' : { $gte: startdate.toISOString() },
'status': { $eq: 'arrived' }
} ,
},
{ $group: {
_id: {id: "$_id", start_time: {$month: "$appointments.start_time"}},
count: { $sum: 1}
}}
])
When I used this :-
{ $group: {
_id: {id: "$_id", start_time: {$month: "$start_time"}},
count: { $sum: 1}
}
}
its showing error message:-
{"name":"MongoError","message":"can't convert from BSON type missing to Date","ok":0,"errmsg":"can't convert from BSON type missing to Date","code":16006,"codeName":"Location16006"}
And when I comment this its showing this :-
Out Put here:-
:[{"count":{"_id":"595b6f95ab43ec1f6c92b898","patient_id":"595649904dbe9525c0e036ef","start_time":"2017-07-04T10:35:00.000Z","status":"arrived"}},
{"count":{"_id":"595dff870960d425d4f14633","patient_id":"5956478b4dbe9525c0e036ea","start_time":"2017-03-08T09:14:00.000Z","status":"arrived"}},{"count":{"_id":"595dffaa0960d425d4f14634","patient_id":"595649904dbe9525c0e036ef","start_time":"2017-03-17T09:15:00.000Z","status":"arrived"}},{"count":{"_id":"595dffcf0960d425d4f14635","patient_id":"595648394dbe9525c0e036ec","start_time":"2017-06-08T09:15:00.000Z","status":"arrived"}},{"count":{"_id":"595dfffb0960d425d4f14636","patient_id":"5956478b4dbe9525c0e036ea","start_time":"2017-06-20T09:16:00.000Z","status":"arrived"}},{"count":{"_id":"595e00160960d425d4f14637","patient_id":"5959ea7f80388b19e0b57817","start_time":"2017-08-17T09:17:00.000Z","status":"arrived"}}]}
const group = {
$group: {
_id: { month: { $month: "$createdAt" } },
count: { $sum: 1 },
},
};
const groups = {
$group: {
_id: null,
patient: { $push: { month: '$_id.month', count: '$count' } },
},
};
return db.Patient.aggregate([group, groups]);
I am trying to compute a percentage in a MongoDB query based on computed fields - not sure if this is possible or not. What I'd like to be able to do is calculate the failure percentage: (failed count / total) * 100
Here are a few sample documents:
{
"_id" : ObjectId("52dda5afe4b0a491abb5407f"),
"type" : "build",
"time" : ISODate("2014-01-20T22:39:43.880Z"),
"data" : {
"buildNumber" : 30,
"buildResult" : "SUCCESS"
}
},
{
"_id" : ObjectId("52dd9fede4b0a491abb5407a"),
"type" : "build",
"time" : ISODate("2014-01-20T22:15:07.901Z"),
"data" : {
"buildNumber" : 4,
"buildResult" : "FAILURE"
}
},
{
"_id" : ObjectId("52dda153e4b0a491abb5407b"),
"type" : "build",
"time" : ISODate("2014-01-20T22:21:07.790Z"),
"data" : {
"buildNumber" : 118,
"buildResult" : "SUCCESS"
}
}
Here is the query I am trying to work with. The issue is in the FailPercent/$divide line:
db.col.aggregate([
{ $match: { "data.buildResult" : { $ne : null } } },
{ $group: {
_id: {
month: { $month: "$time" },
day: { $dayOfMonth: "$time" },
year: { $year: "$time" },
},
Aborted: { $sum: { $cond : [{ $eq : ["$data.buildResult", "ABORTED"]}, 1, 0]} },
Failure: { $sum: { $cond : [{ $eq : ["$data.buildResult", "FAILURE"]}, 1, 0]} },
Unstable: { $sum: { $cond : [{ $eq : ["$data.buildResult", "UNSTABLE"]}, 1, 0]} },
Success: { $sum: { $cond : [{ $eq : ["$data.buildResult", "SUCCESS"]}, 1, 0]} },
Total: { $sum: 1 },
FailPercent: { $divide: [ "Failure", "Total" ] }
} },
{ $sort: { "_id.year": 1, "_id.month": 1, "_id.day": 1 } }
])
You almost got it. Only change that would be required is that you'll have to compute the FailPercent in an additional project phase, because the total is only available after the completion of the group phase. Try this:
db.foo.aggregate([
{ $match: { "data.buildResult" : { $ne : null } } },
{ $group: {
_id: {
month: { $month: "$time" },
day: { $dayOfMonth: "$time" },
year: { $year: "$time" },
},
Aborted: { $sum: { $cond : [{ $eq : ["$data.buildResult", "ABORTED"]}, 1, 0]} },
Failure: { $sum: { $cond : [{ $eq : ["$data.buildResult", "FAILURE"]}, 1, 0]} },
Unstable: { $sum: { $cond : [{ $eq : ["$data.buildResult", "UNSTABLE"]}, 1, 0]} },
Success: { $sum: { $cond : [{ $eq : ["$data.buildResult", "SUCCESS"]}, 1, 0]} },
Total: { $sum: 1 }
} },
{$project:{Aborted:1, Failure:1, Unstable:1, Success:1, Total:1, FailPercent: { $divide: [ "$Failure", "$Total" ]}}},
{ $sort: { "_id.year": 1, "_id.month": 1, "_id.day": 1 } }
])