0

Before I begin, I want to state that no other answers regarding similar questions applied to this one. First consider the following JSON code

  "_id": "1412302013-01-20,11:32:22" 
  "display": [
    {
      "Name": "LOG-11-05-SH-O1.mp4",
      "Type": "Startup",
      "Count": 2,
      "Detail": [
        {
          "Start": "2013-01-20,11:32:22",
          "End": "2013-01-20,11:32:30"
        },
        {
          "Start": "2013-01-20,11:32:22",
          "End": "2013-01-20,11:32:30"
        }
      ]
    },
    {
      "Name": "PUR-12-47-SH-X3.mp4",
      "Type": "Movie",
      "Count": "2",
      "Detail": [
        {
          "Start": "2013-01-20,11:32:22",
          "End": "2013-01-20,11:32:30"
        },
        {
          "Start": "2013-01-20,11:32:22",
          "End": "2013-01-20,11:32:30"
        }
      ]
    },

The ID is basically a combination of a license plate number concatted with a time stamp. What I am trying to do is aggregate the sum for "Count", where the "Name": "LOG-11-05-SH-O1.mp4".

I could not even get simple sums to be aggregated, so trying to aggregate this sum has been impossible. When running my script in NodeJS, I either get undefined or [object Object].

I have no clue what is going wrong as I've been following various online examples.

After trying to use parvin's answer below, I am still getting undefined:

collection.aggregate([
    {$match : {"display.Name" : "LOG-11-05-SH-O1.mp4"}},
    {$unwind : "$display"},
    {$group : {_id : "$_id", 
               name : {$first : "$display.Name"}, 
               total : {$sum : "$display.Count"}
              }   
    }], function(err, result) {
    console.log("Aggregation: " + result.total);
});

I've been trying to format the code like these docs.

2 Answers 2

3

The aggregate callback's result parameter is an array, not a single value. You can see this if you just dump out result:

console.log(result);

So to access total of the first doc in the array:

console.log(result[0].total);

But that's going to be 0 because Count in your doc is a string, not a number. You can't $sum strings.

Because you also only want the total for only the "Name": "LOG-11-05-SH-O1.mp4" elements, you need to add another filter after your $unwind:

collection.aggregate([
    {$match : {"display.Name" : "LOG-11-05-SH-O1.mp4"}},
    {$unwind : "$display"},
    {$match : {"display.Name" : "LOG-11-05-SH-O1.mp4"}},
    {$group : {_id : "$_id", 
               name : {$first : "$display.Name"}, 
               total : {$sum : "$display.Count"}
              }   
    }], function(err, result) {
        console.log(result);
});
Sign up to request clarification or add additional context in comments.

7 Comments

Interesting... so $sum doesn't actually give me a sum, forcing me to create a loop in order to add up all the array values.
@krikara The right way to fix this is to change your data so that Count is a number instead of a string.
Yeah I just changed Count to an actual number. But it still isn't a sum, so I still need the loop. I think that is what confused me the most, because in the Mongo shell it was actually a sum. But NodeJS just takes each individual value and stores it in the array.
Actually, I still didn't reach my goal. The aggregation code "sums" all the Count under display, but I was looking for the sum of the all the Count where "Name": "LOG-11-05-SH-O1.mp4".
@krikara See updated answer. You need to $match again after the $unwind for that.
|
1

You can use the following query to find the total count for the Name = "LOG-11-05-SH-O1.mp4". This is the javascript example, you have to convert it to node.js

db.collection.aggregate(
    {$match : {"display.Name" : "LOG-11-05-SH-O1.mp4"}},
    {$unwind : "$display"},
    {$group : {_id : "$_id", 
               name : {$first : "$display.Name"}, 
               total : {$sum : "$display.Count"}
    }}
)

2 Comments

This is not working for me, I'm getting the following error. TypeError: object is not a function. I've also tried adding function(err, count){ as well as putting it into array format, but it still doesn't seem to work.
I updated my JSON above. The aggregation code "sums" all the Count under display, but I was looking for the sum of the all the Count where "Name": "LOG-11-05-SH-O1.mp4". Would this be possible?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.