My database has a collection with two columns, 'a' and 'b', that can both point to a user. Currently, I am running an aggregate query to match rows where either 'a' or 'b' has a specific value. I want to use $group to group the results based on the column that did not match the input value. The query I tried is as follows:
db.collection.aggregate(
{'$project': {'a': 1, 'b': 1}},
{'$group': {'_id': {'$cond':
[{'$eq': ['$a', DBRef('users', ObjectId('533af99ca41fd238a4c60f3f'))]},
'$b', '$a']}}})
However, this minimal aggregate query fails with the following error message:
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
It seems like Mongo is interpreting the DBRef in an unexpected way. I have searched for similar issues, but have not found any relevant information.