Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs + mongodb error exception: FieldPath 'progress' doesn't start with $

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 $
like image 447
lamfete Avatar asked Oct 27 '25 03:10

lamfete


1 Answers

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
}
}])
like image 138
Sarath Nair Avatar answered Oct 28 '25 19:10

Sarath Nair