[SERVER-22005] Aggregation pipeline does not allow references to DBRef fields Created: 23/Dec/15  Updated: 06/Dec/22  Resolved: 04/Feb/16

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Jeremy Mikola Assignee: Backlog - Query Team (Inactive)
Resolution: Duplicate Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-14466 Support projecting the $id or $ref fo... Closed
Assigned Teams:
Query
Backwards Compatibility: Fully Compatible
Operating System: ALL
Steps To Reproduce:

> 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") }

Participants:

 Description   

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.



 Comments   
Comment by Asya Kamsky [ 04/Feb/16 ]

Duplicate of existing SERVER-14466 - I will move my last comment there.

Comment by Kelsey Schubert [ 23/Dec/15 ]

Thanks Andreas, I'm sending this to the query team.

Comment by Andreas B. [ 23/Dec/15 ]

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.

Generated at Thu Feb 08 03:59:07 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.