简体   繁体   English

使用最新的PHP / MongoDB版本

[英]Working with the latest PHP / MongoDB version

After much of a struggle I managed to install MongoDB as a service and WAMP. 经过多次努力,我成功安装了MongoDB作为服务和WAMP。 Then on start I got a fatal error saying these would not work: 然后在开始时我得到一个致命的错误,说这些不起作用:

$m = new Mongo(...);
$m = new MongoClient(...);

In some previous questions on SO people mentioned using a new class called MongoDB/Driver/Manager. 在之前的一些问题中,人们提到了使用名为MongoDB / Driver / Manager的新类。 I also came across something called MongoDB/Client. 我还遇到了一个名为MongoDB / Client的东西。

As a beginner to MongoDB I now stand rather confused about how to use/connect to a DB and collection. 作为MongoDB的初学者,我现在对如何使用/连接到数据库和集合感到困惑。

I guess I will use: 我猜我会用:

$m = new MongoDB/Driver/Manager(...);

However, 然而,

$db = $m->$dbname; // Seems to cause -> Notice: Undefined Property
$collection = $db->shows; // dito

So all in all what are the difference between MongoDB/Driver/Manager and MongoDB/Client ? 总而言之,MongoDB / Driver / Manager和MongoDB / Client之间有什么区别? And with these new classes how would I correctly connect to a DB or Collection as shown in the previous snippet ? 使用这些新类,我将如何正确连接到数据库或集合,如上一个片段所示? I can't seem to find many examples explaining how to use these new classes, or an up to date correct way of using the new classes for basic functionality. 我似乎无法找到许多解释如何使用这些新类的示例,或者使用新类的基本功能的最新正确方法。 Thanks, 谢谢,

I think I understand what I am confusing. 我想我明白我在困惑。

Using the MongoDB/Driver/Manager class and others, are part of the basic tools available with the PHP MongoDB Driver. 使用MongoDB / Driver / Manager类和其他类是PHP MongoDB驱动程序可用的基本工具的一部分。 I am guessing it is not recommended using them unless you know what you are doing, or you want something relatively customized. 我猜不推荐使用它们,除非你知道你在做什么,或者你想要一些相对定制的东西。

A more recommendable alternative is to install "mongodb/mongodb-^1.xx" with a PHP installer such as Composer, which will give you a MongoDB Library . 更值得推荐的替代方法是使用诸如Composer之类的PHP安装程序安装“mongodb / mongodb- ^ 1.xx”,它将为您提供MongoDB库 This will give you classes such as the MongoDB/Client class. 这将为您提供诸如MongoDB/Client类之类的类。

Once the library has been installed you can connect like so: 安装库后,您可以像这样连接:

<?php 
require 'vendor/autoload.php';

$client = new MongoDB/Client('mongodb://localhost:27017');
// Add URI of MongoDB here
$mydb = $client->mydb; // Add the new DB name or existing DB name here
$collection = $mydb->createCollection('userCollection');
... 
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM