[SERVER-32470] Support for $regex operator in $filter of aggregation pipeline. Created: 28/Dec/17  Updated: 29/Jan/18  Resolved: 16/Jan/18

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

Type: New Feature Priority: Major - P3
Reporter: Abhinandan Kothari Assignee: Asya Kamsky
Resolution: Duplicate Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-11947 Add a regex expression to the aggrega... Closed
Backwards Compatibility: Fully Compatible
Participants:

 Description   

Motivation: There should be support in aggregation pipeline for filtering an array using regex on a text field.

Syntax Proposal :

{ $regex: [ "text", "pattern", "options" ] }

Examples:
Sample Document :

{ _id: 1, array: [ "DBMS", "mongodb", "MongoDB", "MONGODB", "SQL", "NOSQL" ] }

Pipeline :

db.collection.aggregate([ { $project: { filteredArray: { $filter: { input: "$array", as: "item", cond: { $regex: [ "$$item", "mongodb", "i" ] } } } } } ] )

Output :

{ _id: 1, filteredArray: [ "mongodb", "MongoDB", "MONGODB" ] }

 Comments   
Comment by Asya Kamsky [ 16/Jan/18 ]

kothariabhi02 I suppose technically this ticket is a duplicate of SERVER-8892 but if you look at that ticket you will see that we closed it as duplicate of SERVER-11947 which is meant to cover all $regex functionality in aggregation.

The syntax that's proposed there is just that - a proposal, feel free to comment in that ticket if the proposed output will not satisfy your use case. Note that $filter allows you to use all aggregation expressions/operators inside cond so there shouldn't be a need to create a unique/different $regex expression for $filter.

As a side note, the exact example you provided can be handled in current version using:

db.collection.aggregate([ { $project: { filteredArray: { $filter: { input: "$array", as: "item", cond: { $gt: [ {$indexOfCP:[ {$toLower:"$$item"}, "mongodb" ]},-1] } } } } } ] )
{ "_id" : 1, "filteredArray" : [ "mongodb", "MongoDB", "MONGODB" ] }

Comment by Abhinandan Kothari [ 16/Jan/18 ]

Any progress on it?

Comment by Abhinandan Kothari [ 01/Jan/18 ]

Hi Asya Kamsky ,
Thanks for the response. But I do not agree with you.
As according to SERVER-11947, the output will be an array of total matches and their corresponding indexes.
Whereas, requirement mentioned in my ticket is quite different as compared to SERVER-11947.
I simply want an array of text fields to be filtered using a simple REGEX operator as mentioned in above example.
And it is clear that this operator is going to be a comparison operator.

Comment by Asya Kamsky [ 29/Dec/17 ]

This looks like a duplicate of SERVER-11947, yes?

Comment by Kelsey Schubert [ 29/Dec/17 ]

Hi kothariabhi02,

Thank you for the feature request. I've sent it to the Query team for consideration, please continue to watch this ticket for updates.

Kind regards,
Kelsey

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