使用 composr 安装 mongodb 时出错 - 无法使用 mongodb/mongodb 的最新版本 1.16.1,因为它需要 ext-mongodb ^1.16.0 [已关闭]

问题描述 投票:0回答:2

我使用的是 Windows 11,安装了 Composer,并下载了 Mongodb dll 文件。

我正在尝试安装>composer require mongodb/mongodb

有人有更好的方法来连接 php 和 Mongodb。

php mongodb windows composer-php connect
2个回答
0
投票

首先,您需要使用以下 PHP 代码建立与 MongoDB 服务器的连接:

<?PHP
// MongoDB connection parameters
$mongoDBConnection = new 
MongoDB\Driver\Manager("mongodb://localhost:27017");

// Database and Collection
$databaseName = "test_db";
$collectionName = "test_collection";

// Select database and collection
$collection = new MongoDB\Driver\Query(["test_query_filter"], ["limit" => 10]);

现在,将“mongodb://localhost:27017”替换为您的 MongoDB 连接字符串。另外,设置适当的 $databaseName 和 $collectionName 变量。

<?PHP
// Execute this query
$cursor = $mongoDBConnection->executeQuery("$databaseName.$collectionName", $collection);

// Loop through the results
foreach($cursor as $document) {
var_dump($document);
// Process each document as needed
}
?>

在上面的代码中,$cursor 变量包含 MongoDB 查询的结果集。您可以遍历游标来访问查询返回的各个文档。


-3
投票
  1. 检查您的 php 版本文件位于 C:\larragon 中的\php\php-8.1.10-Win32-vs16-x64 / php8ts.dll

  2. 根据php8ts.dll文件从https://pecl.php.net/package/mongodb/1.13.0/windows下载dll文件

  3. 下载 8.1 线程安全 (TS) x64

  4. 您将获得 php_mongodb-1.13.0-8.1-ts-vs16-x64.zip

  5. 复制php_mongodb.dll文件到php的ext文件夹中

  6. C:\larragon 位于\php\php-8.1.10-Win32-vs16-x64 xt

  7. 编辑 C:\larragon 中的\php\php-8.1.10-Win32-vs16-x64 / php.ini 文件并添加

  8. 扩展名=mongodb

  9. 重启服务器

  10. 在 Windows 中安装 Composer

  11. 打开命令提示符

  12. 转到您的项目目录www/

  13. 创建composer.json文件

{ “要求”: { “mongodb/mongodb”:“^1.12” } }

  1. 运行命令>composer require mongodb/mongodb
<?php
require_once 'vendor/autoload.php';
$client = new MongoDB\Client('mongodb://localhost:27017');
$collection = $client->selectCollection('iitj', 'home');
$document = $collection->findOne();
var_dump($document);
© www.soinside.com 2019 - 2024. All rights reserved.