[SERVER-26976] Support $type inside of $in Created: 10/Nov/16  Updated: 06/Dec/22

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

Type: Improvement Priority: Major - P3
Reporter: guru rajan Assignee: Backlog - Query Optimization
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Query Optimization
Participants:

 Description   

db.test.insert({a:1});
db.test.insert({a:"1"});
 
--------------------------------------------------------------------------------------------------------
 
> db.test.find({a:{$type:"string"}})
{ "_id" : ObjectId("582420b599ec5f9dcc617115"), "a" : "1" }
> db.test.find({a:{$type:"number"}})
{ "_id" : ObjectId("582420ad99ec5f9dcc617114"), "a" : 1 }

No issues as of now, How can i select both string and numbers with in query

> db.test.find({a:{$type:{$in:["string","number"]}}})
Error: error: {
        "waitedMS" : NumberLong(0),
        "ok" : 0,
        "errmsg" : "argument to $type is not a number or a string",
        "code" : 14
}



 Comments   
Comment by Asya Kamsky [ 28/Jun/17 ]

There is a workaround available, it's possible to rewrite the query as an "$or":

db.test.find({$or:[{a:{$type:"number"}}, {a:{$type:"string"}}]})

Comment by Kelsey Schubert [ 14/Nov/16 ]

Hi guruskec@gmail.com,

Thanks for opening this ticket, I've modified this ticket to mark it as a feature request to be considered by the Query Team.

The workaround would be to use the following query with $or:

db.test.find({$or: [{a: {$type: "string"}},  {a: {$type: "number"}}]})

Kind regards,
Thomas

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