【问题标题】:php mongodb : Call to undefined method MongoDB::insert() in db.phpphp mongodb : 在 db.php 中调用未定义的方法 MongoDB::insert()
【发布时间】:2010-09-20 01:45:38
【问题描述】:

我正在运行这段代码:

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

    $collection->insert($content);

我正在尝试通过创建一个随机集合来测试 mongohq。

我收到此错误:

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

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

我也在运行 php 5.2.6

有什么问题?谢谢。

【问题讨论】:

    标签: php mongodb mongodb-php


    【解决方案1】:

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

    我没有使用过该扩展,但根据文档,MongoDB 类中不存在该方法。相反,它是MongoCollection::insert。您通过以下方式获得收藏:

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

    (注释行相当于它下面的行。)

    我猜你正在做类似的事情:

    $collection = $mongo->foo;
    $collection->insert(array('x' => 1));
    

    (编辑:我第一次没有看到你的代码 sn-p。这正是你正在做的。)

    我建议您阅读tutorial 了解更多信息。

    【讨论】:

    • 新Mongo末尾的dbname不是db吗?所以我不必选择它两次?
    • 您不会插入数据库。您插入到集合中。 $mongo 实例只是与 mongo 服务器的连接。第一个属性是数据库的名称。第二个是集合的名称。
    猜你喜欢
    • 2019-09-24
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    相关资源
    最近更新 更多