[SERVER-6573] Querying for $minKey/$maxKey returns all documents Created: 24/Jul/12  Updated: 29/Jul/14  Resolved: 29/Jul/14

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

Type: Bug Priority: Major - P3
Reporter: Kristina Chodorow (Inactive) Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-7388 Implement all query operators using M... Closed
Operating System: ALL
Participants:

 Description   

If a collection has, say, 10 chunks and you do db.chunks.find({"min.shardKey":MinKey}), then all 10 chunks will be returned. Same with $maxKey. Seems unintuitive for +/-infinity to match everything.

(Hilarious query for $minKey, recorded for posterity

> // $minKey
> db.chunks.find({$and : [{"min._id" : {$not : {$type : 1}}}, {"min._id" : {$not : {$type : 2}}}, {"min._id" : {$not : {$type : 3}}}, {"min._id" : {$not : {$type : 4}}}, {"min._id" : {$not : {$type : 5}}}, {"min._id" : {$not : {$type : 6}}}, {"min._id" : {$not : {$type : 7}}}, {"min._id" : {$not : {$type : 8}}}, {"min._id" : {$not : {$type : 9}}}, {"min._id" : {$not : {$type : 10}}}, {"min._id" : {$not : {$type : 11}}}, {"min._id" : {$not : {$type : 12}}}, {"min._id" : {$not : {$type : 13}}}, {"min._id" : {$not : {$type : 14}}}, {"min._id" : {$not : {$type : 15}}}, {"min._id" : {$not : {$type : 16}}}, {"min._id" : {$not : {$type : 17}}}, {"min._id" : {$not : {$type : 18}}}, {"min._id" : {$not : {$type : 127}}}]})
> // $maxKey
> db.chunks.find({"min._id":{$type:127}})



 Comments   
Comment by Thomas Rueckstiess [ 29/Jul/14 ]

This is no longer an issue. If I query for MinKey or MaxKey, I get exactly that one matching chunk.

mongos> db.chunks.find({"min._id": MinKey}).pretty()
{
	"_id" : "test.users-_id_MinKey",
	"lastmod" : Timestamp(2, 2),
	"lastmodEpoch" : ObjectId("53d8246c85027e0ee1a0057d"),
	"ns" : "test.users",
	"min" : {
		"_id" : { "$minKey" : 1 }
	},
	"max" : {
		"_id" : NumberLong("-4611686018427387902")
	},
	"shard" : "shard0000"
}
 
mongos> db.chunks.find({"max._id": MaxKey}).pretty()
{
	"_id" : "test.users-_id_4611686018427387902",
	"lastmod" : Timestamp(2, 5),
	"lastmodEpoch" : ObjectId("53d8246c85027e0ee1a0057d"),
	"ns" : "test.users",
	"min" : {
		"_id" : NumberLong("4611686018427387902")
	},
	"max" : {
		"_id" : { "$maxKey" : 1 }
	},
	"shard" : "shard0001"
}

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