I have a partial index on a collection that looks sorta like this:
{ // Fields
"Name" : 1
}
{ // Options
"unique" : true,
"name" : "Name_1",
"partialFilterExpression" : {
"IsArchived" : {
"$eq" : true
}
}
}
I just want to get a count of the number of items that have been indexed by this partial index, just to check if it's working. Is there a way to do that? Or is there another good way to check what all is indexed by a partial index?
MongoDB does not support inspecting the contents of an index in the sense of getting details on the indexed documents.
You can, however, simply run a query against your collection using the same filter as the one in your index in order to get the indexed documents:
db.collection.find({ "IsArchived": { "$eq": true } })
Also, you can add .count()
at the end if you want to get the count:
db.collection.find({ "IsArchived": { "$eq": true } }).count()
In order to see if your index gets used you could run e.g. the following query:
db.collection.find({ "Name": "index check", "IsArchived": true }).explain()
If the index gets used (which should be the case) then issuing the above statement should print something like this somewhere inside the "winningPlan"
field:
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"Name" : "index check"
},
"indexName" : "<your index name>"
If it prints something like this then the index does not get used:
"winningPlan" : {
"stage" : "COLLSCAN",