Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the relationship between real collections' names and those in file system

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??

like image 809
tottishi05 Avatar asked May 13 '15 12:05

tottishi05


3 Answers

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

like image 132
tottishi05 Avatar answered Nov 09 '22 21:11

tottishi05


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.
like image 2
EM0 Avatar answered Nov 09 '22 20:11

EM0


Use this

db.getCollection('collection_name').stats()
like image 1
Moh .S Avatar answered Nov 09 '22 21:11

Moh .S