Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-47034

Output of the operator $type is not correct

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Querying
    • None
    • Fully Compatible
    • ALL

      I have the following collection with name travelers

      { "_id" : 1, "name" : "Dev", "nearestAirport" : "JFK", "lastUpdated" : ISODate("2020-03-20T14:34:06.749Z") } { "_id" : 2, "name" : "Eliot", "nearestAirport" : [ "JFK", "BOS" ], "lastUpdated" : ISODate("2020-03-20T14:34:06.749Z") } { "_id" : 3, "name" : "Jeff", "nearestAirport" : "BOS", "lastUpdated" : ISODate("2020-03-20T14:34:06.749Z") } { "_id" : 4, "name" : "Vijay", "nearestAirport" : "BLR", "lastUpdated" : ISODate("2020-03-20T14:34:06.749Z") } { "_id" : 5, "name" : "Reddy", "nearestAirport" : "HYD", "lastUpdated" : ISODate("2020-03-21T06:07:29.443Z") } { "_id" : 6, "name" : "Vivek", "nearestAirport" : [ "PNQ" ], "lastUpdated" : ISODate("2020-03-21T06:07:29.443Z") } { "_id" : 7, "name" : "Guru", "nearestAirport" : "BOM", "lastUpdated" : ISODate("2020-03-20T14:34:06.749Z") }

      The field type of 'nearestAirport' for id : 2 and 6 is Array and rest are string.

      When I ran the query  db.travelers.find({'nearestAirport':{'$type': 2}}) ( 2 represents type string) i get the following output 

      { "_id" : 1, "name" : "Dev", "nearestAirport" : "JFK", "lastUpdated" : ISODate("2020-03-20T14:34:06.749Z") }

      { "_id" : 2, "name" : "Eliot", "nearestAirport" : [ "JFK", "BOS" ], "lastUpdated" : ISODate("2020-03-20T14:34:06.749Z") }

      { "_id" : 3, "name" : "Jeff", "nearestAirport" : "BOS", "lastUpdated" : ISODate("2020-03-20T14:34:06.749Z") } { "_id" : 4, "name" : "Vijay", "nearestAirport" : "BLR", "lastUpdated" : ISODate("2020-03-20T14:34:06.749Z") } { "_id" : 5, "name" : "Reddy", "nearestAirport" : "HYD", "lastUpdated" : ISODate("2020-03-21T06:07:29.443Z") }

      { "_id" : 6, "name" : "Vivek", "nearestAirport" : [ "PNQ" ], "lastUpdated" : ISODate("2020-03-21T06:07:29.443Z") }

      { "_id" : 7, "name" : "Guru", "nearestAirport" : "BOM", "lastUpdated" : ISODate("2020-03-20T14:34:06.749Z") }

      The output retrieves documents with field type Array also.

      However when i ran the query db.travelers.find({'nearestAirport':{'$type': 4}}) ,  i get only two documents which are id : 2 and 6.

      { "_id" : 2, "name" : "Eliot", "nearestAirport" : [ "JFK", "BOS" ], "lastUpdated" : ISODate("2020-03-20T14:34:06.749Z") } { "_id" : 6, "name" : "Vivek", "nearestAirport" : [ "PNQ" ], "lastUpdated" : ISODate("2020-03-21T06:07:29.443Z") }

      Please advice why the query db.travelers.find({'nearestAirport':{'$type': 2}}) is pulling all documents with the filed type Array also when the condition is to fetch field with type string?

      Let me know if i am missing something?

      Below is the version of the server.

       

            Assignee:
            stephen.steneker@mongodb.com Stennie Steneker (Inactive)
            Reporter:
            vbonline79@gmail.com Vijaya Bhaskar
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: