Reputation: 4163
Say I have a collection, with documents like:
{
_id: 1,
name: "Bob",
skillA: 500,
skillB: 1580,
skillC: 750
}
and each the colleciton has the following indices:
db.mycollection.createIndex({ "skillA": -1 })
db.mycollection.createIndex({ "skillB": -1 })
db.mycollection.createIndex({ "skillC": -1 })
I want to be able to get the position of each index property in their respective index. For example, if skillA
is at the first position in the index, then I would like to find a way to return 0
for that.
I understand if I wanted to get the position of the entire document in the collection, I'd just search for documents with an _id $lt whatever, (assuming _id is auto-incremented), and then just use .count(), but I don't know how to do something like this just for the index collection.
Upvotes: 0
Views: 34
Reputation: 28356
You could leverage a covered query sorted by the field in question, which should use a COUNT_SCAN stage instead of fetching any documents:
db.mycollection.find({skillA: {$gt: 500}},{_id:0,skillA:1}).sort({skillA:-1}).count()
Upvotes: 1