-
Type: Task
-
Resolution: Fixed
-
Priority: Minor - P4
-
Affects Version/s: None
-
Labels:
-
0.1
Description
{$type: 2} finds the documents where the value of the field contains a string. Note that the string can be:
- within an array of strings or
- within an array containing strings as well as other data types or
- just by itself
and {$type: 2} and $type will still return the document containing that array. For example:
//Second document in this collection contains an Array which has Strings as well as an Object and a Number MongoDB Enterprise m0TestingCluster-shard-0:PRIMARY> db.ct.find() { "_id" : ObjectId("5be9f1a183dce127c984e8ca"), "conditions" : "ABC" } { "_id" : ObjectId("5be9f1a183dce127c984e8cb"), "conditions" : [ "XXX", "YYY", 1, ObjectId("5be60a03562d7b721fd9395f") ] } { "_id" : ObjectId("5be9f1a183dce127c984e8cc"), "conditions" : [ 1, 2 ] } { "_id" : ObjectId("5be9f1a183dce127c984e8cd"), "conditions" : [ ] } //{$type: 2} will return the second document as shown here MongoDB Enterprise m0TestingCluster-shard-0:PRIMARY> db.getCollection('ct').find({conditions:{$type: 2}}) { "_id" : ObjectId("5be9f1a183dce127c984e8ca"), "conditions" : "ABC" } { "_id" : ObjectId("5be9f1a183dce127c984e8cb"), "conditions" : [ "XXX", "YYY", 1, ObjectId("5be60a03562d7b721fd9395f") ] }
Currently, our documentation on $type does not mention this behavior. This leads to some confusion, as some users expect {$type: 2} to return documents that contain the string in a field just by itself.
This request is for a doc update, so that our doc clearly mentions the above behavior.
Scope
- Test the behavior above for 4.2, 4.0, and 3.6
- Add a new section under Behavior on String, after 'Available Types'.
- Document the behavior stated above
- Backport to all versions where behavior is consistent
External Review should be someone on Query.