PHP + MongoHQ:MongoCursorException

时间:2010-06-16 10:23:28

标签: php mongodb

我正在尝试使用mongodb和PHP。 为此,我创建了一个MongoHQ实例,但由于某些原因,当我尝试从我的php服务器插入某些内容或任何其他操作时,我收到以下错误:

Fatal error: Uncaught exception 'MongoCursorException' with message 'unauthorized for db [datab] lock type: -1 ' in C:\Program Files\EasyPHP5.3.0\www\application\controllers\Stat.ctrl.php:56 
Stack trace: 
  #0 C:\Program Files\EasyPHP5.3.0\www\application\controllers\Stat.ctrl.php(56): MongoCursor->rewind() 
  #1 C:\Program Files\EasyPHP5.3.0\www\index.php(105): Stat->index() 
  #2 {main} thrown in C:\Program Files\EasyPHP5.3.0\www\application\controllers\Stat.ctrl.php on line 56

有谁知道它的来源?

这是我正在使用的php代码:

$username = 'test';
$password = 'test';

try
{
$link = new Mongo( "flame.mongohq.com:27022/datab -u <".$username."> -p <".$password.">" );

//MongoDB::authenticate ( $username , $password )
//$link = new Mongo();
}
catch(MongoConnectionException $e)
{
die('Could not connect. Check to make sure MongoDB is running.');
}
$db = $link->datab;
$col = $db->order;

try
{
// Insert a document (row) into the collection (table)
$doc = array('login' => 'jsmith', 'password' => ' 5f4dcc3b5aa765', 'email' => 'jsmith@example.com');
$col->insert($doc, true);

$doc = array('login' => 'psmith', 'password' => ' 5f4dcc3b', 'email' => 'psmith@example.com');
$col->insert($doc, true);
}
catch(MongoCursorException $e)
{
echo 'Je suis la!';
}

// Get the id of last insert
$id = $doc['_id'];

// Get all documents
$res = $col->find();

echo 'All documents:<br/>';

foreach($res as $doc)
{
echo '<pre>';
print_r($doc);
echo '</pre>';
}

// Query for the document matching the last insert ID
$doc = $col->findone(array('_id' => $id));

echo 'Single document (_id = $id):<br/><pre>';
print_r($doc);

// Update a document
$col->update(array('_id' => $id), array('$set' => array('password' => 'b497dd1a701a33033620780d')));

// Query the updated docuemnt
$doc = $col->findone(array('_id' => $id));

echo 'Updated docuement:<br/><pre>';
print_r($doc);
echo '</pre>';

1 个答案:

答案 0 :(得分:2)

这不是MongoDB使用的连接格式。请参阅http://www.php.net/manual/en/mongo.construct.php

您可能需要将其更改为:

$m = new Mongo("mongodb://$username:$password@flame.mongohq.com:27022/datab");