Viewing content of an index

Is there a way to view the content of an index? I would like to see if an entry for a document is created or not created into an index.

Do A query using explain() it will tell you if the query is using an index. In the example below we create a document. Check for its existence. Then run explain("executionStats) on the same query. You will see that that executiion stage and winning stage are COLLSCAN. Indicating a collection scan. MongoDB does a collection scan when there is no index.

Now we create an index and rerun the query. Now we will see the stage is IXSCAN. This represents an index scan, i.e. the query is using the index. IXSCAN indicates the document can be found in the index.

> use test
switched to db test
> db.test.drop()
false
> db.test.insertOne({"hello" : "world"})
{
	"acknowledged" : true,
	"insertedId" : ObjectId("5f5f38cee83a779d9f5a4410")
}
> db.test.find({"hello" : "world"})
{ "_id" : ObjectId("5f5f38cee83a779d9f5a4410"), "hello" : "world" }
> db.test.find({"hello" : "world"}).explain("executionStats")
{
	"queryPlanner" : {
		"plannerVersion" : 1,
		"namespace" : "test.test",
		"indexFilterSet" : false,
		"parsedQuery" : {
			"hello" : {
				"$eq" : "world"
			}
		},
		"winningPlan" : {
			"stage" : "COLLSCAN",
			"filter" : {
				"hello" : {
					"$eq" : "world"
				}
			},
			"direction" : "forward"
		},
		"rejectedPlans" : [ ]
	},
	"executionStats" : {
		"executionSuccess" : true,
		"nReturned" : 1,
		"executionTimeMillis" : 0,
		"totalKeysExamined" : 0,
		"totalDocsExamined" : 1,
		"executionStages" : {
			"stage" : "COLLSCAN",
			"filter" : {
				"hello" : {
					"$eq" : "world"
				}
			},
			"nReturned" : 1,
			"executionTimeMillisEstimate" : 0,
			"works" : 3,
			"advanced" : 1,
			"needTime" : 1,
			"needYield" : 0,
			"saveState" : 0,
			"restoreState" : 0,
			"isEOF" : 1,
			"direction" : "forward",
			"docsExamined" : 1
		}
	},
	"serverInfo" : {
		"host" : "JD10Gen.local",
		"port" : 27017,
		"version" : "4.2.7",
		"gitVersion" : "51d9fe12b5d19720e72dcd7db0f2f17dd9a19212"
	},
	"ok" : 1
}
> db.test.createIndex({"hello" : 1 })
{
	"createdCollectionAutomatically" : false,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1
}
> db.test.find({"hello" : "world"}).explain("executionStats")
{
	"queryPlanner" : {
		"plannerVersion" : 1,
		"namespace" : "test.test",
		"indexFilterSet" : false,
		"parsedQuery" : {
			"hello" : {
				"$eq" : "world"
			}
		},
		"winningPlan" : {
			"stage" : "FETCH",
			"inputStage" : {
				"stage" : "IXSCAN",
				"keyPattern" : {
					"hello" : 1
				},
				"indexName" : "hello_1",
				"isMultiKey" : false,
				"multiKeyPaths" : {
					"hello" : [ ]
				},
				"isUnique" : false,
				"isSparse" : false,
				"isPartial" : false,
				"indexVersion" : 2,
				"direction" : "forward",
				"indexBounds" : {
					"hello" : [
						"[\"world\", \"world\"]"
					]
				}
			}
		},
		"rejectedPlans" : [ ]
	},
	"executionStats" : {
		"executionSuccess" : true,
		"nReturned" : 1,
		"executionTimeMillis" : 0,
		"totalKeysExamined" : 1,
		"totalDocsExamined" : 1,
		"executionStages" : {
			"stage" : "FETCH",
			"nReturned" : 1,
			"executionTimeMillisEstimate" : 0,
			"works" : 2,
			"advanced" : 1,
			"needTime" : 0,
			"needYield" : 0,
			"saveState" : 0,
			"restoreState" : 0,
			"isEOF" : 1,
			"docsExamined" : 1,
			"alreadyHasObj" : 0,
			"inputStage" : {
				"stage" : "IXSCAN",
				"nReturned" : 1,
				"executionTimeMillisEstimate" : 0,
				"works" : 2,
				"advanced" : 1,
				"needTime" : 0,
				"needYield" : 0,
				"saveState" : 0,
				"restoreState" : 0,
				"isEOF" : 1,
				"keyPattern" : {
					"hello" : 1
				},
				"indexName" : "hello_1",
				"isMultiKey" : false,
				"multiKeyPaths" : {
					"hello" : [ ]
				},
				"isUnique" : false,
				"isSparse" : false,
				"isPartial" : false,
				"indexVersion" : 2,
				"direction" : "forward",
				"indexBounds" : {
					"hello" : [
						"[\"world\", \"world\"]"
					]
				},
				"keysExamined" : 1,
				"seeks" : 1,
				"dupsTested" : 0,
				"dupsDropped" : 0
			}
		}
	},
	"serverInfo" : {
		"host" : "JD10Gen.local",
		"port" : 27017,
		"version" : "4.2.7",
		"gitVersion" : "51d9fe12b5d19720e72dcd7db0f2f17dd9a19212"
	},
	"ok" : 1
}
>
1 Like

Thank you for the explanation.

1 Like