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

发布于 2024-09-24 17:38:02 字数 607 浏览 7 评论 0原文

我正在运行这段代码:

    $db = new Mongo("mongodb://user:[email protected]:27081/dbname");
    $collection = $db->foobar;

    $collection->insert($content);

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

我收到此错误:

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

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

alt text

我也在运行 php 5.2.6

有什么问题?谢谢。

I'm running this code:

    $db = new Mongo("mongodb://user:[email protected]:27081/dbname");
    $collection = $db->foobar;

    $collection->insert($content);

I'm trying to test mongohq by just creating a random collection.

I'm getting this error:

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

I have the client installed as far as I know:

alt text

I'm also running php 5.2.6

What's the problem? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山色无中 2024-10-01 17:38:02

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

我没有使用过该扩展,但根据文档,该方法在 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));

编辑:我第一次没有看到您的代码片段。这正是)

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

Each DB contains one or many collections. You are trying to insert into the DB, instead of the collection.

I've not used that extension, but that method doesn't exist in the MongoDB class according to the documentation. Instead, it is MongoCollection::insert. You get at a collection by:

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

(The commented line is equivalent to the line below it.)

I'm guessing that you are doing something like:

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

(Edit: I didn't see your code snippet the first time. That is precisely what you are doing.)

I suggest that you read the tutorial for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文