[SERVER-12890] Validate values in query projection Created: 25/Feb/14  Updated: 06/Dec/22

Status: Backlog
Project: Core Server
Component/s: Querying
Affects Version/s: 2.6.0-rc0
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Siyuan Zhou Assignee: Backlog - Query Optimization
Resolution: Unresolved Votes: 1
Labels: 26qa
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
Assigned Teams:
Query Optimization
Participants:

 Description   

When using query projection, only 1/0 and true/false are valid for the values (plus null and undefined maybe):

> db.foo.find({ $text: { $search: "dog" } }, { foo: 1 })
> db.foo.find({ $text: { $search: "dog" } }, { foo: true, _id: 0 })

Other values should be disallowed, for example:

> db.foo.find({ $text: { $search: "dog" } }, { foo: "bar" })
> db.foo.find({ a:1}, { $foo: {} })
> db.foo.find({ $text: { $search: "dog" } }, { $meta: "textScore" })

Especially for text search, $meta: "textScore" can be very confusing.



 Comments   
Comment by David Percy [ 29/Jan/21 ]

This behavior has changed now that we allow expressions: .find({}, {foo: "bar"}) treats "bar" as a constant expression, not a "yes, include this field" flag.

But there are still some confusing cases: .find({}, {foo: 5}) treats 5 as a flag, not an expression. 
{$project: {foo: 5}} does the same thing.

Is it still worth banning some of these cases, or is that too big a language change?

Comment by J Rassi [ 25/Feb/14 ]

Suggest that CanonicalQuery validates projection values similarly to sort.

Current allow list for sort: -1, 1, {$meta: "textScore"}
Suggested allow list for projection: 0, 1, false, true, {$meta: "textScore"}

cc dan@10gen.com, benety.goh

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