...
While the $match stage does allow references to DBRef fields (i.e. $id, $ref, $db), users cannot refer to those fields in $project and $group stages (I haven't tested others). I don't see a reason why this would conflict with field path and system variables, since the "$" in question is not a prefix. Could the assertions be relaxed to allow these DBRef fields to be used in pipelines? I'll note that while storing a referenced document's ID in a basic field (or array for reference-many relationships) may be the general advice for users, we've never formally deprecated use of DBRef (not to be confused with DBPointer, which was deprecated in BSON). The DBRef convention is still widely used in ODM libraries.
asya commented on Thu, 4 Feb 2016 21:34:16 +0000: Duplicate of existing SERVER-14466 - I will move my last comment there. thomas.schubert commented on Wed, 23 Dec 2015 19:00:21 +0000: Thanks Andreas, I'm sending this to the query team. a.braun commented on Wed, 23 Dec 2015 16:23:50 +0000: I haven't tested others This also affects the $lookup stage which was designed to do joins to other documents which are quite frequently referenced by DBRef objects.
> db.foo.drop() true > db.foo.insert({x:new DBRef('foo','bar')}) WriteResult({ "nInserted" : 1 }) > db.foo.find() { "_id" : ObjectId("567ac099ca24f37828635372"), "x" : DBRef("foo", "bar") } > db.foo.find({},{"x.$id":1}) { "_id" : ObjectId("567ac099ca24f37828635372"), "x" : { "$id" : "bar" } } > db.foo.aggregate([{$match:{"x.$id": "bar"}}]) { "_id" : ObjectId("567ac099ca24f37828635372"), "x" : DBRef("foo", "bar") } > db.foo.aggregate([{$match:{"x.$id": "bar"}}, {$project:{"x.$id": 1}}]) assert: command failed: { "ok" : 0, "errmsg" : "FieldPath field names may not start with '$'.", "code" : 16410 } : aggregate failed _getErrorWithCode@src/mongo/shell/utils.js:23:13 doassert@src/mongo/shell/assert.js:13:14 assert.commandWorked@src/mongo/shell/assert.js:266:5 DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1215:5 @(shell):1:1 2015-12-23T10:42:21.414-0500 E QUERY [thread1] Error: command failed: { "ok" : 0, "errmsg" : "FieldPath field names may not start with '$'.", "code" : 16410 } : aggregate failed : _getErrorWithCode@src/mongo/shell/utils.js:23:13 doassert@src/mongo/shell/assert.js:13:14 assert.commandWorked@src/mongo/shell/assert.js:266:5 DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1215:5 @(shell):1:1 > db.foo.aggregate([{$match:{"x.$id": "bar"}}, {$group:{_id:"$x.$id"}}]) assert: command failed: { "ok" : 0, "errmsg" : "FieldPath field names may not start with '$'.", "code" : 16410 } : aggregate failed _getErrorWithCode@src/mongo/shell/utils.js:23:13 doassert@src/mongo/shell/assert.js:13:14 assert.commandWorked@src/mongo/shell/assert.js:266:5 DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1215:5 @(shell):1:1 2015-12-23T10:43:09.515-0500 E QUERY [thread1] Error: command failed: { "ok" : 0, "errmsg" : "FieldPath field names may not start with '$'.", "code" : 16410 } : aggregate failed : _getErrorWithCode@src/mongo/shell/utils.js:23:13 doassert@src/mongo/shell/assert.js:13:14 assert.commandWorked@src/mongo/shell/assert.js:266:5 DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1215:5 @(shell):1:1 > db.foo.aggregate([{$match:{"x.$id": "bar"}}, {$group:{_id:"$_id"}}]) { "_id" : ObjectId("567ac099ca24f37828635372") }