How to identify the which collection and index files from dbpath belongs to which collection or index

Easy way to do this is just enable –directoryperdb & —wiredTigerDirectoryForIndexes while starting the mongod, but still if you’ve multiple collections, indexes in a database you can not figure it out while collection files & index files belongs to the respective collection/index.

Let’s discuss on how to find out files vs collection & files vs index.

Imagine, we have database say dbversity_db created with a collection dbversity_col and indexes created on the fields name, type & website as following.

sh-3.2# mkdir -p /data/db
sh-3.2# pwd
/Users/dbversity/mongodb-osx-x86_64-4.0.2/bin
sh-3.2#
sh-3.2# mongod –dbpath /data/db/ –logpath /data/mognod.log –port 27017 –directoryperdb –wiredTigerDirectoryForIndexes –fork

about to fork child process, waiting until server is ready for connections.
forked process: 10878
child process started successfully, parent exiting
sh-3.2#
sh-3.2# mongo dbversity_db –quiet
>
> db
dbversity_db
>
> db.dbversity_col.createIndex({name:1})
{
“createdCollectionAutomatically” : true,
“numIndexesBefore” : 1,
“numIndexesAfter” : 2,
“ok” : 1
}
>
> db.dbversity_col.createIndex({type:1})
{
“createdCollectionAutomatically” : false,
“numIndexesBefore” : 2,
“numIndexesAfter” : 3,
“ok” : 1
}
> db.dbversity_col.createIndex({website:1})
{
“createdCollectionAutomatically” : false,
“numIndexesBefore” : 3,
“numIndexesAfter” : 4,
“ok” : 1
}
>
> for(var i = 1; i <= 100000 ; i++){db.dbversity_col.insert({“_id” : i , name : “MongoDB”, type : ‘NOSQL’, website: ‘http://dbversity.com’ })}
WriteResult({ “nInserted” : 1 })
>
> db.dbversity_col.count()
100000
>
> db.dbversity_col.findOne()
{
“_id” : 1,
“name” : “MongoDB”,
“type” : “NOSQL”,
“website” : “http://dbversity.com”
}
>
> db.dbversity_col.find().limit(3)
{ “_id” : 1, “name” : “MongoDB”, “type” : “NOSQL”, “website” : “http://dbversity.com” }
{ “_id” : 2, “name” : “MongoDB”, “type” : “NOSQL”, “website” : “http://dbversity.com” }
{ “_id” : 3, “name” : “MongoDB”, “type” : “NOSQL”, “website” : “http://dbversity.com” }
>
> exit

Now let’s look at the database files in the corresponding dbpath, we’re still good with collection files as there’s only collection and one file in the collection folder so we can say that file belongs to dbvesity_col. But for indexes we can not guess that.

 

sh-3.2#
sh-3.2# ll -lhtr /data/db/
total 440
-rw——- 1 root wheel 21B Dec 3 00:53 WiredTiger.lock
-rw——- 1 root wheel 45B Dec 3 00:53 WiredTiger
-rw——- 1 root wheel 4.0K Dec 3 00:53 WiredTigerLAS.wt
drwx—— 5 root wheel 160B Dec 3 00:53 journal
-rw——- 1 root wheel 6B Dec 3 00:53 mongod.lock
-rw——- 1 root wheel 114B Dec 3 00:53 storage.bson
drwx—— 4 root wheel 128B Dec 3 00:53 admin
drwx—— 4 root wheel 128B Dec 3 00:53 local
drwx—— 4 root wheel 128B Dec 3 00:53 config
drwx—— 4 root wheel 128B Dec 3 00:54 dbversity_db
-rw——- 1 root wheel 32K Dec 3 00:55 _mdb_catalog.wt
-rw——- 1 root wheel 36K Dec 3 00:57 sizeStorer.wt
-rw——- 1 root wheel 76K Dec 3 00:57 WiredTiger.wt
-rw——- 1 root wheel 1.0K Dec 3 00:57 WiredTiger.turtle
drwx—— 4 root wheel 128B Dec 3 00:58 diagnostic.data
sh-3.2#
sh-3.2# ll -lhtr /data/db/dbversity_db/
total 0
drwx—— 3 root wheel 96B Dec 3 00:54 collection
drwx—— 6 root wheel 192B Dec 3 00:54 index
sh-3.2#
sh-3.2# ll -lhtr /data/db/dbversity_db/collection/
total 4224
-rw——- 1 root wheel 1.3M Dec 3 00:57 7-3708490287298484807.wt
sh-3.2#
sh-3.2# ll -lhtr /data/db/dbversity_db/index/
total 6432
-rw——- 1 root wheel 372K Dec 3 00:57 10-3708490287298484807.wt
-rw——- 1 root wheel 376K Dec 3 00:57 9-3708490287298484807.wt
-rw——- 1 root wheel 1.2M Dec 3 00:57 8-3708490287298484807.wt
-rw——- 1 root wheel 376K Dec 3 00:57 11-3708490287298484807.wt
sh-3.2#

Now to make it further complicate, let’s create one more new collection say ‘new_col’ in the same database.

sh-3.2# mongo dbversity_db –quiet
>
> for(var i = 1; i <= 1000 ; i++){db.new_col.insert({“_id” : i , name : “MongoDB”, type : ‘NOSQL’, website: ‘http://dbversity.com’ })}
WriteResult({ “nInserted” : 1 })
>
> exit
sh-3.2# ll -lhtr /data/db/dbversity_db/collection/
total 4232
-rw——- 1 root wheel 1.3M Dec 3 00:57 7-3708490287298484807.wt
-rw——- 1 root wheel 4.0K Dec 3 00:59 12-3708490287298484807.wt
sh-3.2#
sh-3.2#

Now we can not guess both collection & index belongs to whom !!

Don’t worry, we have a solution for that.

Here you go !

sh-3.2# mongo dbversity_db –quiet
>
> db.dbversity_col.stats({indexDetails:true}).wiredTiger.uri
statistics:table:dbversity_db/collection/7-3708490287298484807
>
> db.new_col.stats({indexDetails:true}).wiredTiger.uri
statistics:table:dbversity_db/collection/12-3708490287298484807
> exit

sh-3.2# ll -lhtr /data/db/dbversity_db/collection/
total 4280
-rw——- 1 root wheel 1.3M Dec 3 00:57 7-3708490287298484807.wt
-rw——- 1 root wheel 28K Dec 3 01:00 12-3708490287298484807.wt
sh-3.2#
sh-3.2#

sh-3.2# mongo dbversity_db –quiet
>

> db.dbversity_col.stats({indexDetails:true}).indexDetails._id_.uri
statistics:table:dbversity_db/index/8-3708490287298484807
>
> db.dbversity_col.stats({indexDetails:true}).indexDetails.name_1.uri
statistics:table:dbversity_db/index/9-3708490287298484807
>
> db.dbversity_col.stats({indexDetails:true}).indexDetails.type_1.uri
statistics:table:dbversity_db/index/10-3708490287298484807
>
> db.dbversity_col.stats({indexDetails:true}).indexDetails.website_1.uri
statistics:table:dbversity_db/index/11-3708490287298484807
>
>
> db.new_col.stats({indexDetails:true}).indexDetails._id_.uri
statistics:table:dbversity_db/index/13-3708490287298484807
>
>
> exit

 

 

sh-3.2# ll -lhtr /data/db/dbversity_db/index/
total 6480
-rw——- 1 root wheel 372K Dec 3 00:57 10-3708490287298484807.wt
-rw——- 1 root wheel 376K Dec 3 00:57 9-3708490287298484807.wt
-rw——- 1 root wheel 1.2M Dec 3 00:57 8-3708490287298484807.wt
-rw——- 1 root wheel 376K Dec 3 00:57 11-3708490287298484807.wt
-rw——- 1 root wheel 24K Dec 3 01:00 13-3708490287298484807.wt
sh-3.2#

Hope this helps !!

By the way, why do you want to know which index/collection files belongs to which index/files 🙁

…  let’s discuss that in our next post.

  • Ask Question