使用 PHP 在 MongoDB 中插入数据 [英] Insert data in MongoDB using PHP

查看:32
本文介绍了使用 PHP 在 MongoDB 中插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 WAMPphp 5.6 并且想使用 PHP 将文档插入到 MongoDB 中.我是这样做的:

I am on php 5.6 using WAMP and want to insert a document into MongoDB using PHP. I am doing it in this way:

<?php

   require 'vendor/autoload.php';

   $con = new MongoDB\Client("mongodb://localhost:27017");
   echo "successfully";

   $db = $con->selectDatabase('DB');
   echo "Selected";

   $col = $db->selectCollection('myCol');
   $document = array( 
      "name" => "Deny", 
      "password" => "1234"
   );

   $col->insert($document);
   echo "successfully";

   ?>

但它给出了错误

致命错误:调用未定义的方法 MongoDB\Collection::insert() in C:\wamp64\www...

Fatal error: Call to undefined method MongoDB\Collection::insert() in C:\wamp64\www...

我已阅读http://php.net/manual/en/mongocollection.insert.php 并且当我使用相同的插入函数时,它对我不起作用.

I have read http://php.net/manual/en/mongocollection.insert.php and when I use the same insert function, it doesn't work for me.

推荐答案

代替 MongoDB\Client 使用 MongoClient .

这对我有用.

这篇关于使用 PHP 在 MongoDB 中插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆