Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-12194

$type documentation should elaborate on behavior for string types

      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.

            Assignee:
            nathan.contino@mongodb.com Nathan Contino (Inactive)
            Reporter:
            harshad.dhavale@mongodb.com Harshad Dhavale
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              4 years, 33 weeks, 2 days ago