[SERVER-9606] $cond operator should allow $match as a boolean expression Created: 07/May/13  Updated: 06/Dec/22

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

Type: New Feature Priority: Major - P3
Reporter: Dillon Krug Assignee: Backlog - Query Execution
Resolution: Unresolved Votes: 3
Labels: asya, expression, needs-scope
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-8582 Extend document expression language i... Closed
is related to SERVER-34766 Allow $expr or $$field in the $geoNea... Closed
is related to SERVER-38198 Remove the requirement that $geoNear ... Closed
Assigned Teams:
Query Execution
Backwards Compatibility: Fully Compatible
Participants:

 Description   

When using $cond in projections, it would be useful to have $match as a boolean expression.

Use Case:

 
db.objects.aggregate([
	{
		$match: { status: "active" }
	}, {
		$project: {
			name: 1,
			meta: 1,
			tags: 1,
			score: {
				$add: [
					{
						$cond:[{
							$match: { tags: "a tag I like" }
						}, 100, 0]
					}, {
						$cond:[{
							$match: { tags: "a tag I really like" }
						}, 250, 0]
					}, {
						$cond:[{
							$match: { tags: "a tag I dislike" }
						}, -100, 0]
					}, {
						$cond:[{
							$match: { "meta.promoted": true }
						}, 1000, 0]
					}
				]
			}
		}
	},{
		$match: { score: { $gt: 0 } }
	}
])

This would eliminate the need to add other $cond expressions like $in (SERVER-6146)



 Comments   
Comment by Asya Kamsky [ 22/Apr/19 ]

grisanty.carlos@c-ven.com this ticket exists because there are tests that one can do in a match expression that cannot be expressed in an aggregation expression.  $eq is not one of them (in other words equality can be easily done already with $eq).  One such missing example are $geo related match expressions, among others.

 

Comment by Carlos Grisanty [ 17/Apr/19 ]

 

Is there any difference between $eq and $match?

In my case I used the $eq instead of $match.

I think the solution you seek is something like this:

$cond: [ 

{ $eq: [ tags: "a tag I like" ] }

, 100, 0 ]

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