9

I use MongoDB 3.0 with WiredTiger storage engine. When I checked my Mongo files in dbPath, I saw the names of the files with the formats as below: collection-0--4989330656807016483.wt collection-2--4989330656807016483.wt collection-4--4989330656807016483.wt . . . How can I know the relationship between these file names and real collections' names except the way of data size??

tottishi05
  • 557
  • 1
  • 5
  • 13

3 Answers3

6

I have found the way that the command "db.collection.stats()" would show the wiredTiger.metadata.uri which defines the relationship between the collection's logical name and file name the command

tottishi05
  • 557
  • 1
  • 5
  • 13
2

A simple script to find the collection name for a given file name:

function findInDb(dbName, collectionIdToFind) {
    var dbToSearch = db.getSiblingDB(dbName);
    var collectionNames = dbToSearch.getCollectionNames();

    for(var i = 0; i < collectionNames.length; i++){
        var name = collectionNames[i];
        var stats = dbToSearch.getCollection(name).stats();
        var uri = stats.wiredTiger.uri;

        if (uri.endsWith(collectionIdToFind))
            return name;
    }
    
    return null;
}

function findInAllDbs(collectionIdToFind) {
    var adminDb = db.getSiblingDB("admin");
    var dbList = adminDb.runCommand({ "listDatabases": 1 }).databases;

    for (var i in dbList) {
        var found = findInDb(dbList[i].name, collectionIdToFind);
        if (found != null) {
            return dbList[i].name + "." + found;
        }
    }
    
    return "(not found)";
}

print(findInAllDbs("collection-20-571885508699163146")); // filename in crash report, etc.
EM0
  • 5,369
  • 7
  • 51
  • 85
1

Use this

db.getCollection('collection_name').stats()
Moh .S
  • 1,920
  • 19
  • 19