[SERVER-12055] Use better bounds (like undefined) when matching empty array Created: 11/Dec/13  Updated: 26/Jan/15  Resolved: 26/Jan/15

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

Type: Bug Priority: Major - P3
Reporter: Scott Hernandez (Inactive) Assignee: David Storch
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-10026 New query system Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Participants:

 Description   

When searching for an empty array if we match on "undefined" in the index we can use the index to find them. Also, searching for $size:0 could also be re-written to the empty array, and use the same bounds.

Currently searching for an empty array with an index on the array does this:

// 2.4.x
find({a:[]}).explain()
{
	"cursor" : "BtreeCursor a_1 multi",
	"isMultiKey" : false,
	"n" : 1,
	"nscannedObjects" : 1,
	"nscanned" : 1,
	"nscannedObjectsAllPlans" : 1,
	"nscannedAllPlans" : 1,
	"scanAndOrder" : false,
	"indexOnly" : false,
	"nYields" : 0,
	"nChunkSkips" : 0,
	"millis" : 0,
	"indexBounds" : {
		"a" : [
			[ null, null ],
			[ [], [] ]
		]}}
 
// 2.5.5--pre (12/11/13)
{
	"cursor" : "BtreeCursor a_1",
	"isMultiKey" : false,
	"n" : 1,
	"nscannedObjects" : 2,
	"nscanned" : 2,
	"nscannedObjectsAllPlans" : 4,
	"nscannedAllPlans" : 4,
	"scanAndOrder" : false,
	"indexOnly" : false,
	"nYields" : 1,
	"nChunkSkips" : 0,
	"millis" : 2,
	"indexBounds" : {
		"a" : [[{
					"$minElement" : 1
				},
				{
					"$maxElement" : 1
				}]]}}



 Comments   
Comment by David Storch [ 26/Jan/15 ]

Both in the 2.6 branch and in master, the query planner's index bounds builder has a special case for matching against an empty array:

https://github.com/mongodb/mongo/blob/r3.0.0-rc6/src/mongo/db/query/index_bounds_builder.cpp#L859-L864

Note that although the bounds appear as [ [null, null], [ [], [] ] ] in the shell, the planner is actually constructing the bounds [ [undefined, undefined], [ [], [] ] ].

This was resolved during the 2.5 development series in commit ce97325d under SERVER-10026. Resolving as a duplicate of SERVER-10026.

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