Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-22005

Aggregation pipeline does not allow references to DBRef fields

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Aggregation Framework
    • Labels:
      None
    • Query
    • Fully Compatible
    • ALL
    • Hide
      > 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") }
      
      Show
      > 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") }

      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.

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            jmikola@mongodb.com Jeremy Mikola
            Votes:
            1 Vote for this issue
            Watchers:
            11 Start watching this issue

              Created:
              Updated:
              Resolved: