4

I'm trying to modify the second pipeline from this query (which I got from here nodejs + mongoose - query aggregate

db.todos.aggregate([
{ 
  "$group": { 
      "_id": "$pic",             
      "open_count": {
          "$sum": {
              "$cond": [ { "$eq": [ "$status", "open" ] }, 1, 0 ]
          }
      },
      "progress_count": {
          "$sum": {
              "$cond": [ { "$eq": [ "$status", "progress" ] }, 1, 0 ]
          }
      },
      "done_count": {
          "$sum": {
              "$cond": [ { "$eq": [ "$status", "done" ] }, 1, 0 ]
          }
      },
      "archive_count": {
          "$sum": {
              "$cond": [ { "$eq": [ "$status", "archive" ] }, 1, 0 ]
          }
      } 
  }  
},
{
  "$group": {
      "_id": "$_id",            
      "detail": {
          "$push": {
              "name": "open",
              "$todos": "$open_count"
          },
          "$push": {
              "name": "progress",
              "$todos": "$progress_count"
          },
          "$push": {
              "name": "done",
              "$todos": "$done_count"
          },
          "$push": {
              "name": "archive",
              "$todos": "$archive_count"
          }
      }
  }
},
{
  "$project": {
      "_id": 0, "pic": "$_id", "detail": 1
  }
}
])

I want this kind of JSON structure so I can put it on google chart, which the format is like this:

[
    {
        "pic": "A",
        "detail": [
            {
                "name": "open",
                "todos": 2
            },
            {
                "name": "progress",
                "todos": 1
            },
            {
                "name": "done",
                "todos": 8
            },
            {
                "name": "archive",
                "todos": 20
            }
        ],
        "pic": "B",
        "detail": [
            {
                "name": "open",
                "todos": 5
            },
            {
                "name": "progress",
                "todos": 2
            },
            {
                "name": "done",
                "todos": 5
            },
            {
                "name": "archive",
                "todos": 10
            }
        ],
    }
]

But I got this error

exception: FieldPath 'progress' doesn't start with $
2
  • 1
    Any chance it points to a line number? YOu have a few instances of progress in there Commented Nov 24, 2015 at 15:35
  • @DavidGrinberg no, there is no information of line number in the error message. Commented Nov 25, 2015 at 1:28

1 Answer 1

2

Try with this aggregation query:

db.todos.aggregate([
  {
  "$group": {
    "_id": {
      "pic": "$pic",
      "name": "$status"
    },
    "todos": {
      "$sum": 1
    }
  }
},
{
  "$project": {
    "_id": 0,
    "pic": "$_id.pic",
    "detail": {
      "name": "$_id.name",
      "todos": "$todos"
    }
  }
},
{
  "$group": {
    "_id": "$pic",
    "detail": {
      "$push": "$detail"
    }
  }
},
{
  "$project": {
    "_id": 0, "pic": "$_id", "detail": 1
}
}])
Sign up to request clarification or add additional context in comments.

2 Comments

your aggregation query only sum status: "open". Another status ("progress", "done", and "archive") is 0. How to sum another status and add it into the result?
Oops my bad. Edited the answer

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.