具有mongoDB逻辑的虚拟文件夹


virtual folders with mongoDB logic

需要一些逻辑帮助。我有一个文件夹,我将通过http://php/post上传很多文件。当它们上传时,我将把它们索引到MongoDB中的一个集合中。我需要将文件组织到不同的文件夹中,实际上不会有文件夹,但当有人查看我将要组合的界面时,它看起来就像一个文件树。我还将设置虚拟文件夹的权限。我一直在反复思考如何将文件索引到Mongo Collection的虚拟文件夹中,我遇到的问题是为每个用户分配权限。我正在考虑索引这样的文件和虚拟文件夹(很抱歉代码太长,只是想让你知道文件树的深度):

$collection->insert(array(
    '_id' => new MongoID(),
    'type' => 'folder',
    'folderOrFileNameAndLocation' => 'products'
));
$collection->insert(array(
    '_id' => new MongoID(),
    'type' => 'folder',
    'folderOrFileNameAndLocation' => 'products/device'
));
$collection->insert(array(
        '_id' => new MongoID(),
        'type' => 'folder',
        'folderOrFileNameAndLocation' => 'products/device/manuals'
    ));
$collection->insert(array(
        '_id' => new MongoID(),
        'type' => 'file',
        'fileName' => 'theManual.pdf',
        'location' => 'products/device/manuals/'
    ));

我的问题是,当我添加用户时,如何为每个用户分配文件夹权限,以下是我的想法

$collection->insert(array(
        '_id' => new MongoID(),
        'username' => 'Joe',
        'password' => '1234', //will obviously be one way hashed
        //STORE AS A STRING
        'permissibleFolders' => 'products/device/manuals/, software/latest/'
    ));
    //OR SHOULD I DO IT THIS WAY???
    $collection->insert(array(
        '_id' => new MongoID(),
        'username' => 'Joe',
        'password' => '1234', //will obviously be one way hashed
        'permissibleFolders' => array(
            //DO I HAVE TO SET A KEY FOR THIS OR CAN I JUST ADD THEM IN?
            products/device/manuals/,
            software/latest/
        )
    ));

现在,通过这种方式,我将如何查询分配给用户的文件夹?这就是我的想法,但我该如何查询最后一部分?我可以让它与嵌套查询一起工作,但这是我永远不会做的事情

$pulledData = $collection->find(array('username' => $username, 'password' => $password));
        $doesExist = $pulledData -> count();
        if($doesExist == 1){
            logUserInAndStuff();
            foreach($pulledData as $row){
                $accessibleFolders = $row['permissibleFolders'];
            }
            //HOW DO I MAKE THIS QUERY THE PROPER FOLDERS?
            $pulledData = $collection->find(array('folderNameAndLocation'=>$accessibleFolders));
        }

很抱歉,如果我的逻辑很愚蠢,我对服务器的"拉动"和允许我做的事情都很有限。我什么都没有得到,希望我能完成一些伟大的任务。有人能帮我想出任何更好的方法都会很棒!我对noSQL还很陌生,我可能想的结构太多了

为什么不向每个文件系统对象添加允许用户的列表

为用户添加权限很容易

db.collection.update({folderOrFileNameAndLocation : 'products'}, {$push: {allowedUsers: 'Joe'}})

删除用户很容易

db.collection.update({folderOrFileNameAndLocation : 'products'}, {$pull: {allowedUsers: 'Joe'}})

检查权限很容易

db.collection.find({folderOrFileNameAndLocation : 'products', allowedUsers : 'Joe'})

列出用户可以访问的所有内容是很容易的

db.collection.find({allowedUsers: 'Joe'}, {folderOrFileNameAndLocation: 1, _id: 0})

您可能会发现有一件事很有帮助,那就是将文件和文件夹构建为树,而不是将位置设置为字符串。所以你会有

$collection->insert(array(
    '_id' => new MongoID(),
    'type' => 'folder',
    'folderOrFileName' => 'products'
));

然后创建子文件夹:

$collection->find(array('folderOrFileName' => 'products'));
$collection->insert(array(
    '_id' => new MongoID(),
    'type' => 'folder',
    'folderOrFileName' => 'device',
    'parent' => $collection['_id']
));

这种结构有优点也有缺点;取决于你还需要支持什么,你可能会发现自己的方式或树状方式更容易。但我绝对建议将用户名分配给文件夹,而不是反之亦然。