[SERVER-25667] How to get the number of key/value pairs in an index? Created: 17/Aug/16  Updated: 17/Aug/16  Resolved: 17/Aug/16

Status: Closed
Project: Core Server
Component/s: Admin, Index Maintenance, Shell, WiredTiger
Affects Version/s: 3.3.5
Fix Version/s: None

Type: Question Priority: Trivial - P5
Reporter: George Thompson Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

Windows 10
MS VS 2015
MongoDB 3.3.5
MongoDB C++11 Driver 3.0.1
Using WiredTiger

I want the statistic

"btree" { "number of key/value pairs"}

which is equal to "btree_entries" from bt_stat.c. (I recognize this is "really, really expensive").

I run this in mongo:

db.adminCommand({ setParameter: 1, "wiredTigerEngineRuntimeConfig" : "create,statistics=(all)"})

and mongod reports it was successful:

2016-08-17T11:18:00.960-0700 I STORAGE  [conn5176] Reconfiguring WiredTiger storage engine with config string: "statistics=(all)"

which, presumably, sets WT_CONN_STAT_ALL so that btree_entries will be collected.

According to the documentation, I have to use the db.adminCommand. "You can only set this parameter using the setParameter command and not using the command line or configuration file option."

But when I then run:

db.employees.stats({'indexDetails':true, 'indexDetailsName': "employeenum_1_check_1"})

I still get:

"number of key/value pairs" : 0,



 Comments   
Comment by Daniel Pasette (Inactive) [ 17/Aug/16 ]

Here's the documentation link:
http://mongodb.github.io/mongo-cxx-driver/classmongocxx_1_1database.html#a1ce8581871d060bce35739a682fc35e9

Comment by George Thompson [ 17/Aug/16 ]

C++11 Driver 3.0.1 – I believe the runCommand will do the trick.

Comment by Daniel Pasette (Inactive) [ 17/Aug/16 ]

which driver are you using?

Comment by George Thompson [ 17/Aug/16 ]

Thanks pasette. Now I'll go and figure out how to run that with the C++ driver.

Comment by Daniel Pasette (Inactive) [ 17/Aug/16 ]

Hi George,
I think the best way to get the number of keys in an index is to use the validate command and check the "keysPerIndex" subdocument.

> db.bar.validate(true)
{
	"ns" : "test.bar",
	"nInvalidDocuments" : NumberLong(0),
	"nrecords" : 2,
	"nIndexes" : 1,
	"keysPerIndex" : {
		"test.bar.$_id_" : 2
	},
	"indexDetails" : {
		"test.bar.$_id_" : {
			"valid" : true
		}
	},
	"valid" : true,
	"warnings" : [ ],
	"errors" : [ ],
	"ok" : 1
}

Generated at Thu Feb 08 04:09:50 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.