[DOCS-2562] Comment on: "manual/core/query-optimization.txt" Created: 20/Jan/14  Updated: 03/Nov/17  Resolved: 21/Jan/14

Status: Closed
Project: Documentation
Component/s: None
Affects Version/s: None
Fix Version/s: 01112017-cleanup

Type: Improvement Priority: Major - P3
Reporter: Matt Johnson Assignee: Kay Kim (Inactive)
Resolution: Won't Fix Votes: 0
Labels: collector-298ba4e7
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Location: http://docs.mongodb.org/manual/core/query-optimization/
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; BRI/2; InfoPath.3; rv:11.0) like Gecko
Referrer: http://docs.mongodb.org/manual/core/crud/
Screen Resolution: 1920 x 1200
repo: docs
source: core/query-optimization


Participants:
Days since reply: 10 years, 4 weeks, 2 days ago

 Description   

I think the examples for covering a query are backwards on this page.



 Comments   
Comment by Kay Kim (Inactive) [ 21/Jan/14 ]

Hi Matt –
I believe the example queries are correct.

You can verify via the following:

> db.inventory.insert( { item: "cookies", type: "food" } )
> db.inventory.ensureIndex( { type: 1, item: 1 } )

Run the first query with the explain and you can see that the indexOnly field is true :

note: the projection document {item: 1, _id: 0} indicates that the item field should return and that the _id field should not return ( 0 in the projection document suppresses the field). When you specify {item: 1}, this automatically suppresses all other field except for the _id field, so you must include _id: 0 in the projection document as well to suppress the field from the result set

> db.inventory.find( { type: "food", item:/^c/ },
...                    { item: 1, _id: 0 } ).explain()
{
	"cursor" : "BtreeCursor type_1_item_1 multi",
	"isMultiKey" : false,
	"n" : 1,
	"nscannedObjects" : 0,
	"nscanned" : 1,
	"nscannedObjectsAllPlans" : 0,
	"nscannedAllPlans" : 1,
	"scanAndOrder" : false,
	"indexOnly" : true,
	"nYields" : 0,
	"nChunkSkips" : 0,
	"millis" : 0,
	"indexBounds" : {
		"type" : [
			[
				"food",
				"food"
			]
		],
		"item" : [
			[
				"c",
				"d"
			],
			[
				/^c/,
				/^c/
			]
		]
	},
	"server" : "bartleby.local:27019"
}

The second query has indexOnly equal to false, because in the projection document, we specify return the item field, but we do not explicitly state to not return the _id field with the _id: 0 specification

> db.inventory.find( { type: "food", item:/^c/ },
...                    { item: 1 } ).explain()
{
	"cursor" : "BtreeCursor type_1_item_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" : {
		"type" : [
			[
				"food",
				"food"
			]
		],
		"item" : [
			[
				"c",
				"d"
			],
			[
				/^c/,
				/^c/
			]
		]
	},
	"server" : "bartleby.local:27019"
}

Hope this helps.

Generated at Thu Feb 08 07:43:44 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.