[SERVER-13366]  Add Support to Mongo : Query Filter on Substring of a Field Created: 26/Mar/14  Updated: 10/Dec/14  Resolved: 28/Mar/14

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

Type: Improvement Priority: Major - P3
Reporter: Kovilendra [X] Assignee: Unassigned
Resolution: Done Votes: 0
Labels: FieldName, filter, substring
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

It would help if MongoDB could support queries where you can filter on regex / substring of fields
Eg.
Sql :

Select * from
Table
where Substring(FieldName , 1 , 3) = 'abc' ;

We use examples of this in our Current Sql Rules Environment and need to have this in Mongo.

Thanks
Kovi



 Comments   
Comment by Daniel Pasette (Inactive) [ 28/Mar/14 ]

You should be able to get what you need from the Aggregation Framework and the $substr operator.

Here's an example:

> db.collection.drop()
true
> db.collection.insert({longString: "abcdefgh"}); 
WriteResult({ "nInserted" : 1 })
> db.collection.aggregate({$match: {}}, {$project:{a: {$substr:["$longString",0,3]}}}, {$match: {a: 'abc'}} )
{ "_id" : ObjectId("5335d3261d971cf798f8f957"), "a" : "abc" }

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