Pergunta

I have a collection where either of two columns, 'a' or 'b' can point to a user. I am performing an aggregate which matches rows with either 'a' or 'b' having a particular value, and the natural thing to do would be to use $group such that the _id of each output row is the value of whichever column did not match the input (as the presence of the input user as either 'a' or 'b' is obviously implied, and the columns have no special meaning). This query seems to me like it ought to work:

db.collection.aggregate(
     {'$project': {'a': 1, 'b': 1}},
     {'$group': {'_id': {'$cond': 
            [{'$eq': ['$a', DBRef('users', ObjectId('533af99ca41fd238a4c60f3f'))]},
                 '$b', '$a']}}})

That's the aggregate reduced to the bare minimum. It fails with:

Fri Apr 25 01:31:28.140 aggregate failed: {
    "errmsg" : "exception: invalid operator '$ref'",
    "code" : 15999,
    "ok" : 0
} at src/mongo/shell/collection.js:898

Clearly Mongo is interpolating the DBRef. I can't find any references (ha!) to a similar issue.

Foi útil?

Solução

You can't use DBRef in an aggregation pipeline. From the Aggregation Guide:

Aggregation Pipeline Limits

Aggregation operations with the aggregate command have the following limitations.

Type Restrictions The aggregation pipeline (page 7) cannot operate on values of the following types: Symbol, MinKey, MaxKey, DBRef, Code, and CodeWScope.

http://docs.mongodb.org/manual/MongoDB-aggregation-guide.pdf

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top