[SERVER-44717] MinKey > MaxKey in agg language Created: 18/Nov/19  Updated: 06/Dec/22  Resolved: 19/Nov/19

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

Type: Task Priority: Major - P3
Reporter: Patrick Meredith Assignee: Backlog - Query Team (Inactive)
Resolution: Duplicate Votes: 0
Labels: qopt-team
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-19171 Shell does not always use extended JS... Closed
Assigned Teams:
Query
Participants:

 Description   

I was checking the sanity of how we have been merging missing and null using {$lte: ['$foo', null]}. On the bright side, it works, on the down side it appears that minKey is > maxKey (and all other values).

> db.mk.find()
{ "_id" : ObjectId("5dd2faf88a7fad5be8c370d0"), "max" : { "$maxKey" : 1 }, "min" : { "$minKey" : 1 } }
> db.mk.aggregate({$project: {min:1, max:1, out: {$lt: ['$min', '$max']}}})
{ "_id" : ObjectId("5dd2faf88a7fad5be8c370d0"), "max" : { "$maxKey" : 1 }, "min" : { "$minKey" : 1 }, "out" : false }
> db.mk.aggregate({$project: {min:1, max:1, out: {$gt: ['$min', '$max']}}})
{ "_id" : ObjectId("5dd2faf88a7fad5be8c370d0"), "max" : { "$maxKey" : 1 }, "min" : { "$minKey" : 1 }, "out" : true }

It appears related to this:
https://github.com/mongodb/mongo/blob/990b2ef30d7b8fa4db6af6d79da80e3664df9c21/src/mongo/bson/bsontypes.h#L67

My assumption is that somewhere we are treating BSONType as unsigned, so that -1 becomes MAX_INT.



 Comments   
Comment by Patrick Meredith [ 19/Nov/19 ]

kevin.pulocraig.homaYep, looks like this is it! Much better than having to track down where something would have been accidentally cast to unsigned. The new shell should presumably fix these issues once it's done.

Comment by Craig Homa [ 19/Nov/19 ]

Hey patrick.meredith, looks like Kevin diagnosed the issue and provided the work-around. Please give that a try and let us know if you are still having an issue with it.

Comment by Kevin Pulo [ 18/Nov/19 ]

This is SERVER-19171 again. They have used sub-objects { $minKey/$maxKey: 1 } instead of the actual BSON MinKey/MaxKey:

> db.mk.insert({_id:1, min:MinKey, max:MaxKey})
WriteResult({ "nInserted" : 1 })
> db.mk.insert({_id:2, min:{$minKey:1}, max:{$maxKey:1}})
WriteResult({ "nInserted" : 1 })
> db.mk.aggregate({$project: {min:1, max:1, out: {$lt: ['$min', '$max']}}})
{ "_id" : 1, "min" : { "$minKey" : 1 }, "max" : { "$maxKey" : 1 }, "out" : true }
{ "_id" : 2, "min" : { "$minKey" : 1 }, "max" : { "$maxKey" : 1 }, "out" : false }
> db.mk.aggregate({$project: {min:1, max:1, out: {$gt: ['$min', '$max']}}})
{ "_id" : 1, "min" : { "$minKey" : 1 }, "max" : { "$maxKey" : 1 }, "out" : false }
{ "_id" : 2, "min" : { "$minKey" : 1 }, "max" : { "$maxKey" : 1 }, "out" : true }

Comment by Carl Champain (Inactive) [ 18/Nov/19 ]

Passing this ticket along to the Query team.

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