[SERVER-47034] Output of the operator $type is not correct Created: 21/Mar/20  Updated: 21/Mar/20  Resolved: 21/Mar/20

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

Type: Bug Priority: Major - P3
Reporter: Vijaya Bhaskar Assignee: Stennie Steneker (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File image-2020-03-21-13-09-51-404.png    
Backwards Compatibility: Fully Compatible
Operating System: ALL
Participants:

 Description   

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.

 



 Comments   
Comment by Stennie Steneker (Inactive) [ 21/Mar/20 ]

Hi vbonline79@gmail.com,

This $type matching outcome for arrays is expected and documented:

For documents where field is an array, $type returns documents in which at least one array element matches a type passed to $type.

Please note that the SERVER project is for reporting bugs or feature suggestions for the MongoDB server. For questions about using MongoDB products or services, please use the MongoDB Community Forums.

Regards,
Stennie

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