title: MongoDB Aggregation Error "each item in the pipeline must be a document"
body: <p>I have a MongoDB aggregation that looks like this:</p>
  
  <pre><code>[
      {
          "$match": [
              {"Created": {"$gte": ISODate("2014-01-10T00:00:00Z")}}
          ]
      },
      {
          "$group":
          {
              "_id": [
                  {"year": {"$year": "Created"}},
                  {"month": {"$month": "Created"}},
                  {"day": {"$dayOfMonth": "Created"}}
              ],
              "count": {"$sum": 1}
          }
      }
  ]
  </code></pre>
  
  <p>When I run this query in MongoVUE, it returns the following error:</p>
  
  <pre>Incorrect syntax in pipeline
  Each item in the pipeline must be a document
  Type: System.Exception
  Stack:    at MangoUI.ComAggregate.kRemove_Click(Object sender, EventArgs e)</pre>
  
  <p>I'm completely stumped, and from my Googling so is the rest of the Intenet-at-large. The query worked correctly before I added the date created $match operator. It's possible that the error belongs to MongoVUE and not MongoDB itself.</p>
  
  <p>The relevant portion of the schema looks like this:</p>
  
  <pre><code>{
    "_id" : new BinData(3, "m13wFpp9gUi09cRCuG43aw=="),
    "Created" : ISODate("2013-12-19T01:00:20.972Z")
  }
  </code></pre>
  
  <p>Can anybody help me find the cause of this error? I'm afraid I'm completely stumped.</p>
  
  <hr>
  
  <p>I'm not allowed to answer my own question (because apparently it's utterly impossible to find the answer yourself in under 8 hours if your StackOverflow reputation is too low? Huh?) So I'm posting the answer below:</p>
  
  <p>Ok I found the answer, as far as I can determine it is indeed a bug (or at least an omission and a misleading error message) in MongoVUE. Writing the same query (with orid's modifications to it) in this way works:</p>
  
  <pre><code>{
    "$match": {
      "Created": {
        "$gte": ISODate("2014-01-10T00:00:00Z")
      }
    }
  },
  {
    "$group": {
      "_id": {
        "year": {
          "$year": "$Created"
        },
        "month": {
          "$month": "$Created"
        },
        "day": {
          "$dayOfMonth": "$Created"
        }
      },
      "count": {
        "$sum": 1
      }
    }
  }
  </code></pre>
  
  <p>(Note the only change I really made was removing the array markers around it.)</p>
  
  <p>Additionally, that error appears <em>any</em> time you put an aggregation with more than one operation in an array in MongoVUE. If you leave off the array, the operations work fine. If you add it, suddenly you get "each item in the pipeline must be a document."</p>