[SERVER-7785] Cannot project embedded object field names prefixed with dollar sign Created: 28/Nov/12  Updated: 06/Dec/22  Resolved: 25/Apr/16

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 2.2.1
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Jeremy Mikola Assignee: Backlog - Query Team (Inactive)
Resolution: Done Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-828 Support for selecting array elements ... Closed
Assigned Teams:
Query
Operating System: ALL
Participants:

 Description   

After SERVER-828, it's not possible to specify a field with $ in the field as part of the projection. This is likely an edge case not handled by the Projection::validateQuery() implementation here: https://github.com/mongodb/mongo/commit/fb66c84bc7bc1ece63a65766bfea2f797f3b7121

> db.foo.drop()
> db.foo.save({a:{$id: new ObjectId()}});
> db.foo.find({}, {'a.$id': 1})
error: {
	"$err" : "Positional operator does not match the query specifier.",
	"code" : 16354
}

The reproduction steps are a bit contrived, but the practical use case is a projection of the $id field of a DBRef object.



 Comments   
Comment by Ramon Fernandez Marina [ 25/Apr/16 ]

Looks like this behavior went away in 2.6.0, most probably due to the rewrite of the query subsystem for MongoDB 2.6, so I'm closing this ticket.

Comment by Asya Kamsky [ 25/Apr/16 ]

I'm not sure as of which version, but in 3.0.4, 3.2.4 and 3.3.4 the find with projection works:

db.foo.find({}, {'a.$id': 1})
{ "_id" : ObjectId("571e574ef47c42773c316e20"), "a" : { "$id" : ObjectId("571e574ef47c42773c316e1f") } }

Comment by Johan De Meersman [ 19/Jan/16 ]

Another, possibly more common usecase, is selecting in db.system.profiles inside the command.query document, which almost always holds $ operators as keys.

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