php mongodb:在db.php中调用未定义的方法MongoDB :: insert()

JSN*_*bie 9 php mongodb mongodb-php

我正在运行此代码:

    $db = new Mongo("mongodb://user:pw@flame.mongohq.com:27081/dbname");
    $collection = $db->foobar;

    $collection->insert($content);
Run Code Online (Sandbox Code Playgroud)

我试图通过创建一个随机集合来测试mongohq.

我收到这个错误:

Fatal error:  Call to undefined method MongoDB::insert() in /ajax/db.php on line 24
Run Code Online (Sandbox Code Playgroud)

据我所知,我安装了客户端:

替代文字

我也在运行php 5.2.6

有什么问题?谢谢.

Mat*_*hew 13

每个DB包含一个或多个集合.您正尝试插入数据库,而不是集合.

我没有使用该扩展,但MongoDB根据文档,该方法在类中不存在.相反,它是MongoCollection::insert.你得到一个集合:

// $collection = $mongo->selectDB("foo")->selectCollection("bar");
$collection = $mongo->foo->bar; 
$collection->insert(array('x' => 1));
Run Code Online (Sandbox Code Playgroud)

(注释行等同于它下面的行.)

我猜你做的是:

$collection = $mongo->foo;
$collection->insert(array('x' => 1));
Run Code Online (Sandbox Code Playgroud)

(编辑:我没有第一次看到你的代码片段.这正是你正在做的事情.)

我建议您阅读教程以获取更多信息.