[SERVER-14595] Allow {$regex: /pattern/} syntax inside $in (currently only /pattern/ syntax is allowed) Created: 17/Jul/14  Updated: 06/Dec/22

Status: Backlog
Project: Core Server
Component/s: Querying
Affects Version/s: 2.4.10, 2.6.3, 2.7.3
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Stephen Lee Assignee: Backlog - Query Optimization
Resolution: Unresolved Votes: 7
Labels: query-44-grooming
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to DOCS-3775 Documentation for regex under an $in ... Closed
related to SERVER-13779 Allow $not to be applied to $regex (c... Closed
is related to SERVER-55639 $in with regex has special meaning Backlog
Assigned Teams:
Query Optimization
Participants:

 Description   

The following two queries should be equivalent:

coll.find({a: {$in: [/abc/]}})
coll.find({a: {$in: [{$regex: /abc/}]}})

However, in 2.6.3 the former succeeds and the latter fails with "Can't canonicalize query: BadValue cannot nest $ under $in". We should support both forms of this query.

Original Description

Per this documentation, I assume that querying with the $in operator upon a list of regular expressions using either form of regular expression syntax, /^pattern/ or {$regex: "^pattern"}, is equivalent. However, the latter $regex operator syntax doesn't work.

v2.4.10 returns no documents, but v2.6.3 returns the 17287 error.

v2.7 nightly (mongodb-osx-x86_64-2b55561913f26870b0d4b1c8ec180139819eeba6-2014-07-16) also returns the 17287 error.



 Comments   
Comment by David Storch [ 02/Aug/19 ]

This limitation still exists as of version 4.2:

MongoDB Enterprise > db.c.find({a: {$in: [{$regex: "abc"}]}})
Error: error: {
	"ok" : 0,
	"errmsg" : "cannot nest $ under $in",
	"code" : 2,
	"codeName" : "BadValue"
}

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