0
votes

I have a document structure as follows:

{
  "_id" : NumberLong("80000000012"),
[...]
  "categories" : [{
      "parent" : "MANUFACTURER",
      "category" : "Chevrolet"
    }, {
      "parent" : "MISCELLANEOUS",
      "category" : "Miscellaneous"
    }],
[...]
}

I am trying to get a distinct list of all 'category' fields for each 'parent' field. I was trying to utilize the aggregation framework to do this with the following query:

db.posts_temp.aggregate(
    {$unwind : '$categories'},
    {$match : {'categories.parent' : 'MISCELLANEOUS'}},
    {$project : {
        '_id' : 0,
        parent : '$categories.parent',
        category : '$categories.category'
        }
    },
    {
        $group : { 
            _id : '$parent',
            category : {$addToSet : '$category'}
        }
    }
);

Running this query returns the following error:

{
    "errmsg" : "exception: $unwind:  value at end of field path must be an array",
    "code" : 15978,
    "ok" : 0
}

This seems to be tied to the group portion of the query, because, when I remove it, the query runs correctly, but, obviously, the data is not where I want it to be.

1

1 Answers

0
votes

I just tried executing the above aggregation query on my mongo instance. Here are my 3 documents each with a key of categories that has an array of two nested documents.

Here is my data:

{
    "_id" : ObjectId("512d5252b748191fefbd4698"),
    "categories" : [
        {
            "parent" : "MANUFACTURER",
            "category" : "Chevrolet"
        },
        {
            "parent" : "MISCELLANEOUS",
            "category" : "Miscellaneous"
        }
    ]
}
{
    "_id" : ObjectId("512d535cb748191fefbd4699"),
    "categories" : [
        {
            "parent" : "MANUFACTURER",
            "category" : "Chevrolet"
        },
        {
            "parent" : "MISCELLANEOUS",
            "category" : "Pickup"
        }
    ]
}
{
    "_id" : ObjectId("512d536eb748191fefbd469a"),
    "categories" : [
        {
            "parent" : "MANUFACTURER",
            "category" : "Toyota"
        },
        {
            "parent" : "MISCELLANEOUS",
            "category" : "Miscellaneous"
        }
    ]
}

Here is the aggregation query of yours that I ran:

db.posts_temp.aggregate( {$unwind:'$categories'} , {$match: {'categories.parent':'MISCELLANEOUS'}}, {$project:{'_id':0, parent: '$categories.parent', category:'$categories.category'}}, {$group:{_id:'$parent', category:{$addToSet:'$category'}}})

Here is the result:

{
    "result" : [
        {
            "_id" : "MISCELLANEOUS",
            "category" : [
                "Pickup",
                "Miscellaneous"
            ]
        }
    ],
    "ok" : 1
}

Let me know if there some discrepancies between my data and yours.

CSharpie