[SERVER-2702] Compare document fields in queries Created: 08/Mar/11 Updated: 05/Dec/17 Resolved: 05/Oct/17 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Querying |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | New Feature | Priority: | Minor - P4 |
| Reporter: | Remon van Vliet | Assignee: | Tess Avitabile (Inactive) |
| Resolution: | Duplicate | Votes: | 35 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||||||||||||||||||||||||||
| Backwards Compatibility: | Fully Compatible | ||||||||||||||||||||||||||||||||||||
| Sprint: | Query 2017-10-23 | ||||||||||||||||||||||||||||||||||||
| Participants: | |||||||||||||||||||||||||||||||||||||
| Description |
|
It would be rather handy to do things like : db.test.find({a:{$gt:b}}) In other words, be able to query based on document fields comparison. Any plans for this? |
| Comments |
| Comment by David Storch [ 06/Oct/17 ] | ||||||
|
The feature described by tess.avitabile above was implemented in https://docs.mongodb.com/master/reference/operator/query/expr/ | ||||||
| Comment by Tess Avitabile (Inactive) [ 05/Oct/17 ] | ||||||
|
This can now be done using $expr, which allows you to use aggregation expressions in the match language:
| ||||||
| Comment by Jonathan Rezende [ 09/Oct/16 ] | ||||||
|
weird, this seem to be something so simple. It would be very helpful with the validator option. | ||||||
| Comment by Remon van Vliet [ 09/Jan/12 ] | ||||||
|
I don't think that'll work since that's would be rather ambiguous syntax. One wouldn't be able to perform an actual $gt on the string "$b" without some sort of escaping or other mechanism that allows special casing $-first strings. I haven't looked at the aggregation framework yet so I'm not sure if that has similar issues. In any case, for this particular feature request I don't think the {$gt:"$b"} approach is preferable. EDIT: I've reviewed the aggregation framework syntax and theoretically it suffers from the same problem although I can't see any direct issues so far since the expressions/pipeline operators where it would matter do not allow field references it seems (e.g. you can't {$match:{field1:"$field2"}} by the looks of it). Obviously having to compare with the string "$field" rather than the field "field" is an edge case but that can hardly be a valid argument in favor of the current syntax. Hoping that a user doesn't do something that should be possible isn't really a sound design philosophy. | ||||||
| Comment by Matt Vogel [ 08/Jan/12 ] | ||||||
|
Personally, I like the direction we're going with the aggregation framework which would look more like this: db.test.find({a:{$gt:"$b"}}) | ||||||
| Comment by Remon van Vliet [ 09/Mar/11 ] | ||||||
|
By the way, the above example isn't valid JSON obviously so it would probably need an operator of some sort, e.g. : db.test.find({a:{$gt:{$field: "b"}}}) |