2016-05-18 6 views
0

私は単純なMongoDBをセットアップし、いくつかのデータを挿入しようとしています。私はPHPを使用する必要があり、私は私のuniersity(それは私にとって義務である、サーバーはすべての設定を持っている)が所有する外部サーバー上でコードを実行しています。私の問題は、tutorialsPointのコードを使用していて、すべてが '$list = $db->listCollections();'に達するまでうまくいくことです。それから私は、次のログを受け取る:PHP致命的なエラー、MongoDB、カーソル

PHPの致命的なエラー:メッセージでキャッチされない例外「にMongoCursorExceptionは」「localhostの:27017:Test.mycol

にクエリに対して許可されていない私の問題は、なぜこの私にはわからないということですそれがどのように解決されるのかを示します。助言がありますか?

コード:

<?php 
    // connect to mongodb 
    # $m = new MongoClient(); 
    $mongo = new Mongo(); 
    $db = $mongo->Test; 
    $username="4rebacz"; 
    $password="pass"; 
    $db->authenticate($username, $password); 
    echo "Connection to database successfully"; 

    // select a database 
    # $db = $m->mydb; 
    echo "Database mydb selected"; 
    $collection = $db->mycol; 
    echo "Collection selected succsessfully"; 

    $document = array( 
     "title" => "MongoDB", 
     "description" => "database", 
     "likes" => 100, 
     "url" => "http://www.tutorialspoint.com/mongodb/", 
     "by", "tutorials point" 
    ); 

    $collection->insert($document); 
    echo "Document inserted successfully"; 
    $cursor = $collection->find(); 

    $str = file_get_contents('dirdata/formdata.txt'); 
    $json = json_decode($str, true); 
    foreach($json as $id => $item) 
    { 
    $collection->insert($item); 
    } 

    $list = $db->listCollections(); 
    foreach($list as $collection) 
    { 
    echo "$collection \n"; 
    } 
?> 

答えて

0

listCollections - 必要な管理ロール Read there

Every database includes the following database administration roles:

dbAdmin Provides the following actions on the database’s system.indexes, system.namespaces, and system.profile collections:

  • collStats
  • dbHash
  • dbStats
  • find
  • killCursors
  • listIndexes
  • listCollections
  • dropCollection and createCollection on system.profile only
+0

OK、私はもう一つ質問を有することができますか?管理役割を使用せずにDBにすべてのレコードを表示するにはどうすればよいですか? –

+0

少なくとも、すべてのコレクションの名前を知る必要があります。現在のユーザーには、このコレクションの権利が必要です... –

+0

そうでない場合は、ユーザー "admin"を使用するか、ユーザーの権利を追加してください –