MongoDB Sum-Query does not sum - mongodb

I have the following documents:
{ "_id" : ObjectId("5d9db4462034bf17454d7d33"), "name" : "Product1", "cost_oneoff" : "1", "cost_monthly" : "1", "margin_oneoff" : "1", "margin_monthly" : "1", "price_oneoff" : "1", "price_monthly" : "1" }
{ "_id" : ObjectId("5d9dc2f2d8e17309b46f9b03"), "name" : "Product2", "cost_oneoff" : "0", "cost_monthly" : "1", "margin_oneoff" : "0,5", "margin_monthly" : "0,5", "price_oneoff" : "0", "price_monthly" : "2" }
I want the sum of e.g. cost monthly with the following statement:
{ "_id" : null, "total" : 0 }
Can someone help me?
db.service_items.aggregate([
{ $match: {$or: [{"_id": ObjectId("5d9db4462034bf17454d7d33")},{"_id": ObjectId("5d9dc2f2d8e17309b46f9b03")}]}},
{ $group:
{_id: null,
total: {
$sum: "$cost_monthly"
}
}
}
])
Result:
{ "_id" : null, "total" : 0 }
The desired answer is 2

The $sum operator only works on an integer. According to the docs it ignores non-numeric values. You seem to have them stored as a string. Change cost_monthly to an integer and you should get the desired result:
"cost_monthly" : 1
You can check it out here.

Like #silencedogood said, The $sum operator only works on an integer. We need to convert the string to a numeric value using $toInt operator.
The following is an example:
db.service_items.aggregate([
{
$match: {
$or: [
{
"_id": ObjectId("5d9db4462034bf17454d7d33")
},
{
"_id": ObjectId("5d9dc2f2d8e17309b46f9b03")
}
]
}
},
{
$group: {
"_id": null,
"total": {
$sum: {
$toInt: "$cost_monthly"
}
}
}
}
])
Note: The $toInt is introduced in Mongo v4.0

Related

How to get the recent values of grouped result?

Below is the document which has an array name datum and I want to filter the records based on StatusCode, group by Year and sum the amount value from the recent record of distinct Types.
{
"_id" : ObjectId("5fce46ca6ac9808276dfeb8c"),
"year" : 2018,
"datum" : [
{
"StatusCode" : "A",
"Type" : "1",
"Amount" : NumberDecimal("100"),
"Date" : ISODate("2018-05-30T00:46:12.784Z")
},
{
"StatusCode" : "A",
"Type" : "1",
"Amount" : NumberDecimal("300"),
"Date" : ISODate("2023-05-30T00:46:12.784Z")
},
{
"StatusCode" : "A",
"Type" : "2",
"Amount" : NumberDecimal("420"),
"Date" : ISODate("2032-05-30T00:46:12.784Z")
},
{
"StatusCode" : "B",
"Type" : "2",
"Amount" : NumberDecimal("420"),
"Date" : ISODate("2032-05-30T00:46:12.784Z")
}
]
}
In my case following is the expected result :
{
Total : 720
}
I want to achieve the result in the following aggregate Query pattern
db.collection.aggregate([
{
$addFields: {
datum: {
$reduce: {
input: "$datum",
initialValue: {},
"in": {
$cond: [
{
$and: [
{ $in: ["$$this.StatusCode", ["A"]] }
]
},
"$$this",
"$$value"
]
}
}
}
}
},
{
$group: {
_id: "$year",
RecentValue: { $sum: "$datum.Amount" }
}
}
])
You can first $unwind the datum array. Do the filtering and sort by the date. Then get the record with latest datum by a $group. Finally do another $group to calculate the sum.
Here is a mongo playground for your reference.

How to use $group and $cond together in MongoDB Aggregation?

I have collection like below.
{
"userId" : "1",
"feedbackGiven" : true
}
{
"userId" : "1",
"feedbackGiven" : false
}
{
"userId" : "1",
"feedbackGiven" : true
}
{
"userId" : "2",
"feedbackGiven" : false
}
{
"userId" : "2",
"feedbackGiven" : true
}
I need to group this on userId and get two values as count of totalGivenFeedback and count of false feedbackGiven.
I tried below query.
db.collection.aggregate([
{
$group: { _id: "$userId", feedbackGiven: { $push : "$feedbackGiven"} }
}
])
This gives results like below.
{
"_id" : "1",
"feedbackGiven" : [
true,
false,
true
]
}
{
"_id" : "2",
"feedbackGiven" : [
false,
true
]
}
I can get total feedbackGiven and count of false feedbackGiven using above results from my JavaScript code.
But my question, Is there a way to get it using MongoDB query.
I am expecting results like below.
{
"_id" : "1",
"totalFeedbackGive" : 3,
"falseFeedbackCount" : 1
}
{
"_id" : "2",
"totalFeedbackGive" : 1,
"falseFeedbackCount" : 1
}
Can anyone give me a solution ?
You can use below aggregation
db.collection.aggregate([
{ "$group": {
"_id": "$userId",
"totalFeedbackGive": { "$sum": 1 },
"falseFeedbackCount": {
"$sum": {
"$cond": [
{ "$eq": ["$feedbackGiven", false] },
1,
0
]
}
}
}}
])
So you need to use $sum accumulator to count the number of documents after applying $group stage.
And second you need to use $sum accumulator $conditionally to count the number of documents for the falseFeedback count

Count of MongoDB aggregation match results

I'm working with a MongoDB collection that has a lot of duplicate keys. I regularly do aggregation queries to find out what those duplicates are, so that I can dig in and find out what is and isn't different about them.
Unfortunately the database is huge and duplicates are often intentional. What I'd like to do is to find the count of keys that have duplicates, instead of printing a result with thousands of lines of output. Is this possible?
(Side Note: I do all of my querying through the shell, so solutions that don't require external tools or a lot of code would be preferred, but I understand that's not always possible.)
Example Records:
{ "_id" : 1, "type" : "example", "key" : "111111", "value" : "abc" }
{ "_id" : 2, "type" : "example", "key" : "222222", "value" : "def" }
{ "_id" : 3, "type" : "example", "key" : "222222", "value" : "ghi" }
{ "_id" : 4, "type" : "example", "key" : "333333", "value" : "jkl" }
{ "_id" : 5, "type" : "example", "key" : "333333", "value" : "mno" }
{ "_id" : 6, "type" : "example", "key" : "333333", "value" : "pqr" }
{ "_id" : 7, "type" : "example", "key" : "444444", "value" : "stu" }
{ "_id" : 8, "type" : "example", "key" : "444444", "value" : "vwx" }
{ "_id" : 9, "type" : "example", "key" : "444444", "value" : "yz1" }
{ "_id" : 10, "type" : "example", "key" : "444444", "value" : "234" }
Here is the query that I've been using to find duplicates based on key:
db.collection.aggregate([
{
$match: {
type: "example"
}
},
{
$group: {
_id: "$key",
count: {
$sum: 1
}
}
},
{
$match: {
count: {
$gt: 1
}
}
}
])
Which gives me an output of:
{
"_id": "222222",
"count": 2
},
{
"_id": "333333",
"count": 3
},
{
"_id": "444444",
"count": 4
}
The result I want to get instead:
3
You are almost there, just missing the last $count:
db.collection.aggregate([
{
$match: {
type: "example"
}
},
{
$group: {
_id: "$key",
count: {
$sum: 1
}
}
},
{
$match: {
count: {
$gt: 1
}
}
},
{
$count: "count"
}
])
Akrion's answer seems to be correct, but I can't test it because we're on an older version of MongoDB. A coworker gave me an alternative solution that works on 3.2 (not sure about other versions).
Adding .toArray() will convert the results to an array, and you can then get the size of the array using .length.
db.collection.aggregate([
{
$match: {
type: "example"
}
},
{
$group: {
_id: "$key",
count: {
$sum: 1
}
}
},
{
$match: {
count: {
$gt: 1
}
}
}
]).toArray().length

Get Individual results of Mongodb Average of Two columns

I have a dataset like t
{
"_id" : ObjectId("5a867bae000e4f1c9c77d36d"),
"userid" : "5a20ee1acdacc7086ce7742d",
"sprice" : null,
"lprice" : 4.2,
"fruit" : "#Apple",
"createdate" : ISODate("2018-02-16T06:35:26.285Z"),
"__v" : 0
},
{
"_id" : ObjectId("5a867bae000e4f1c9c77d36e"),
"userid" : "5a20ee1acdacc7086ce7742e",
"sprice" : 3.5,
"lprice" : null,
"fruit" : "#Apple",
"createdate" : ISODate("2018-02-16T06:35:26.285Z"),
"__v" : 0
},
{
"_id" : ObjectId("5a867bae000e4f1c9c77d36e"),
"userid" : "5a20ee1acdacc7086ce7742e",
"sprice" : 8.6,
"lprice" : 2.2,
"fruit" : "#Apple",
"createdate" : ISODate("2018-02-16T06:35:26.285Z"),
"__v" : 0
}
for this I have to calculate the Average sprice of '#Apple' and neglect those entry which have value NULL.
For this My query is like this which returns exactly what I want, i.e
db.Collection.aggregate([
{ "$match": {
"fruit": "#Apple",
"sprice": {$ne:null}
}},
{ "$group": {
"_id": null,
"sprice": { "$avg": "$sprice" }
}}
])
It gives me the result. Now my Question is If I want to get the Individual Result of sprice and lprice then How My query is modified.
Expected answer will be like this:
{ "_id" : null, "sprice" : 6.05 } // Already Get from this query
{ "_id" : null, "lprice" : 3.2 } //Desired Result.
Any Help is Appreciated
With $facet
db.Collection.aggregate([
{ "$match": { "fruit": "#Apple" } },
{
"$facet": {
"sprice": [
{ "$match": { "sprice": { "$ne": null } } },
{ "$group": {
"_id": null,
"sprice": { "$avg": "$sprice" }
}}
],
"lprice": [
{ "$match": { "lprice": { "$ne": null } } },
{ "$group": {
"_id": null,
"lprice": { "$avg": "$lprice" }
}}
]
}
}
])
Sample Output
[
{ "sprice": { "_id" : null, "sprice" : 6.05 } },
{ "lprice": { "_id" : null, "lprice" : 3.2 } }
]
$avg ignores non numeric values by default so no explicit null filter is required, below pipeline will give you the desired results
db.Collection.aggregate([
{ "$match": {
"fruit": "#Apple"
}},
{ "$group": {
"_id": null,
"sprice": { "$avg": "$sprice" },
"lprice": { "$avg": "$lprice" }
}}
])

MongoDb aggregation framework value of a field where max another field

I have a collection that has records looking like this:
"_id" : ObjectId("550424ef2f44472856286d56"), "accountId" : "123",
"contactOperations" :
[
{ "contactId" : "1", "operation" : 1, "date" : 500 },
{ "contactId" : "1", "operation" : 2, "date" : 501 },
{ "contactId" : "2", "operation" : 1, "date" : 502 }
]
}
I want to know the latest operation number that has been applied on a certain contact.
I'm using the aggregation framework to first unwind the contactOperations and then grouping by accountId and contactOperations.contactId and max contactOperations.date.
aggregate([{$unwind : "$contactOperations"}, {$group : {"_id":{"accountId":"$accountId", "contactId":"$contactOperations.contactId"}, "date":{$max:"$contactOperations.date"} }}])
The result I get is:
"_id" : { "accountId" : "123", "contactId" : "2" }, "time" : 502 }
"_id" : { "accountId" : "123", "contactId" : "1" }, "time" : 501 }
Which seems correct so far, but I also need the contactOperations.operation field that was recorded with $max date. How can I select that?
You have to sort the unwind values then apply $last operator to get operation for max date. Hope this query will solve your problem.
aggregate([
{
$unwind: "$contactOperations"
},
{
$sort: {
"date": 1
}
},
{
$group: {
"_id": {
"accountId": "$accountId",
"contactId": "$contactOperations.contactId"
},
"date": {
$max: "$contactOperations.date"
},
"operationId": {
$last: "$contactOperations.operation"
}
}
}
])