Search code examples
mongodbmongodb-indexes

In Mongodb how can I view an index's values


I not asking how to view the indexes on a collection but how can I look inside the index and see its values?

I have a field that should be unique so I created a unique index and now I want to cross verify that all the documents are present in the index.


Solution

  • Normally you cannot look inside the index. It's just linked list. But... You can do count from index. db.data.find({},{"_id":1}).hint({"_id":1}).itcount()

    In that example I project only field _id, with hint() I ordered system use unique index of "_id" and with itcount() I ordered NOT to use metadata information of count, but go thru that find cursor and do count of every item.