[SERVER-7621] Missing operator to compare two fields in aggregation framework query Created: 12/Nov/12  Updated: 15/Feb/13  Resolved: 12/Nov/12

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

Type: Improvement Priority: Major - P3
Reporter: Joe Davis Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

There doesn't seem to be a way to include a $match condition that compares two fields eg to include all documents where field1 == field2:

{ "$match" : { "$field1" : { "$eq" : "$field2"}} }

We can only use operators that compare a field against values.

$where is no longer allowed in aggregate framework queries, so one cannot use arbitrary javascript to achieve this.



 Comments   
Comment by Joe Davis [ 12/Nov/12 ]

Aaron - that's rad. Thanks for pointing that out. Lesson learned, read the code.

Comment by Aaron Staple [ 12/Nov/12 ]

Joe - you can use the same $project strategy with any of the aggregation expressions. <http://docs.mongodb.org/manual/reference/aggregation/#expressions>

Comment by Joe Davis [ 12/Nov/12 ]

Eliot - awesome that would be great.

Aaron - thanks for the info. I'll try it out, wonder if that works for other operators besides $eq though, as I'm looking for less than, less than equals, greater than, etc. as well.

Thanks again all, looking forward to the expression based filters.

Comment by Eliot Horowitz (Inactive) [ 12/Nov/12 ]

Sorry, should've explained more.
There is another case (I'll try to dig it up, but made SERVER-7623 as well) to make all comparison operators take expressions on the right hand side.
So the combination solves this both for agg and regular queries.

Comment by Aaron Staple [ 12/Nov/12 ]

Hi Joe,

Currently what you'd need to do for this is $project, then $match

aggregate( { $project:{ matches:

{ $eq:[ '$field1', '$field2' ] }

} }, { $match:

{ matches:true }

} )

Comment by Joe Davis [ 12/Nov/12 ]

That's cool but has nothing to do with my issue.

This is about a lack of operators to compare fields: eg $eg, $gte, $lte, $lt, etc.

All mongo operators should be able to take fields or values as right hand side operator.

Currently, nothing allows to compare field1 against field2 (only field1 against a constant value).

Comment by Eliot Horowitz (Inactive) [ 12/Nov/12 ]

The agg framework uses the same query language as regular find.
The case for adding $eq is here: SERVER-1367

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