Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB Aggregation Error "each item in the pipeline must be a document"

I have a MongoDB aggregation that looks like this:

[
    {
        "$match": [
            {"Created": {"$gte": ISODate("2014-01-10T00:00:00Z")}}
        ]
    },
    {
        "$group":
        {
            "_id": [
                {"year": {"$year": "Created"}},
                {"month": {"$month": "Created"}},
                {"day": {"$dayOfMonth": "Created"}}
            ],
            "count": {"$sum": 1}
        }
    }
]

When I run this query in MongoVUE, it returns the following error:

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)

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.

The relevant portion of the schema looks like this:

{
  "_id" : new BinData(3, "m13wFpp9gUi09cRCuG43aw=="),
  "Created" : ISODate("2013-12-19T01:00:20.972Z")
}

Can anybody help me find the cause of this error? I'm afraid I'm completely stumped.


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:

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:

{
  "$match": {
    "Created": {
      "$gte": ISODate("2014-01-10T00:00:00Z")
    }
  }
},
{
  "$group": {
    "_id": {
      "year": {
        "$year": "$Created"
      },
      "month": {
        "$month": "$Created"
      },
      "day": {
        "$dayOfMonth": "$Created"
      }
    },
    "count": {
      "$sum": 1
    }
  }
}

(Note the only change I really made was removing the array markers around it.)

Additionally, that error appears any 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."

like image 837
james.schend Avatar asked Jan 13 '14 19:01

james.schend


1 Answers

pipeline = [{
  "$match": {
    "Created": {
      "$gte": ISODate("2014-01-10T00:00:00Z")
    }
  }
},
{
  "$group": {
    "_id": {
      "year": {
        "$year": "$Created"
      },
      "month": {
        "$month": "$Created"
      },
      "day": {
        "$dayOfMonth": "$Created"
      }
    },
    "count": {
      "$sum": 1
    }
  }
}]

Document.objects.aggregate(*pipeline)
like image 150
2 revs, 2 users 95% Avatar answered Oct 29 '22 03:10

2 revs, 2 users 95%