使用 php 连接 mongo db 的字段


Concatenating fields of mongo db using php

我正在尝试使用 php 连接 mongo db 中的两个字段以获得庞大的数据集。以下是我到目前为止找到并尝试的示例解决方案,但这在 php 中不起作用。任何人都可以帮助进行必要的更正(如果有的话)在下面的代码中。

var pipeline = [
        {
           "$project": {
            "data1": 1,
            "data2": { $concat: [ "$data1", " ", "$data2" ] }
            }
        }
],
cursor = db.collection.aggregate(pipeline),
bulkUpdateOps = cursor.map(function (doc) { 
    return { 
            "updateOne": {
                "filter": { "_id": doc._id },
                "update": { "$set": { "data2": doc.data2 } } 
             }
          };
});         
db.collection.bulkWrite(bulkUpdateOps);

我正在使用作曲家命令按照您在评论中的建议安装 mongo-php-library:

$composer require "mongodb/mongodb=^1.0.0" 

但它没有用.php MONGO 驱动程序是 1.6.12。 而 MONGO DB 版本是 3.2.3 . 错误如下:

 Your version of PHP, 5.4.16, is affected by CVE-2013-6420 and cannot safely perform certificate validation, we strongly suggest you upgrade.
  ./composer.json has been created
 Loading composer repositories with package information
 Updating dependencies (including require-dev)
 Your requirements could not be resolved to an installable set of packages.
  Problem 1
     - mongodb/mongodb 1.0.1 requires ext-mongodb ^1.1.0 -> the requested PHP extension mongodb is missing from your system.
     - mongodb/mongodb 1.0.0 requires ext-mongodb ^1.1.0 -> the requested PHP extension mongodb is missing from your system.
- Installation request for mongodb/mongodb ^1.0.0 -> satisfiable by mongodb/mongodb[1.0.0, 1.0.1].
  To enable extensions, verify that they are enabled in those .ini files:
- /etc/php.ini
- /etc/php.d/curl.ini
- /etc/php.d/dom.ini
- /etc/php.d/fileinfo.ini
- /etc/php.d/json.ini
- /etc/php.d/mbstring.ini
- /etc/php.d/mysql.ini
- /etc/php.d/mysqli.ini
- /etc/php.d/pdo.ini
- /etc/php.d/pdo_mysql.ini
- /etc/php.d/pdo_sqlite.ini
- /etc/php.d/phar.ini
- /etc/php.d/posix.ini
- /etc/php.d/sqlite3.ini
- /etc/php.d/sysvmsg.ini
- /etc/php.d/sysvsem.ini
- /etc/php.d/sysvshm.ini
- /etc/php.d/wddx.ini
- /etc/php.d/xmlreader.ini
- /etc/php.d/xmlwriter.ini
- /etc/php.d/xsl.ini
- /etc/php.d/zip.ini
  You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.
   Installation failed, deleting ./composer.json.

我需要帮助安装此库。我是否安装了错误的版本?

谢谢大家的帮助,但我无法安装 mongo php 库,所以上面的解决方案对我不起作用。我找到了另一种我想分享的方式来合并两个使用 shell 命令的字段。在 php 文件中:

$concat_Command = "mongo < concat.sql";
shell_exec($concat_Command);

康卡特.sql

 use dbname;
 db.collection.find().forEach(function(doc){
    db.collection.update(
        { "_id": doc._id },
        { "$set": { "description": doc.title+" "+doc.description } }
    )
});

如果没有看到你在 PHP 中解决这个问题的尝试,下面的大部分内容都是基于假设的,因此可能无法提供最佳解决方案,但仍然可以作为指导,引导你朝着正确的方向前进以解决您的问题。

将 PHP 驱动程序与免费的 mongo-php-library(在裸机驱动程序之上实现功能更全的 API)结合使用,将聚合管道和后续批量更新操作构造为:

$pipeline = [["$project" => ["data1" => 1, "data2" => ["$concat" => [ "$data1", " ", "$data2" ]]]]];
$operation = new Aggregate($databaseName, $collectionName, $pipeline, ['typeMap' => $typeMap]);
$cursor = $operation->execute($primaryServer);
$results = iterator_to_array($cursor);
function mapper($doc) {
    return [
        "updateOne" => [
            ["_id" => $doc["_id"]], 
            [
                "$set" => [
                    "data2" => $doc["_id"]
                ]
            ]
        ]
    ]
};
$ops = array_map("mapper", $results);
$bulkUpdateOps = new BulkWrite($databaseName, $collectionName, $ops);
$writeResult = $bulkUpdateOps->execute($primaryServer);

在上面,$primaryServerBulkWrite execute()方法的参数,它应该包含MongoDB'Driver'Server对象的实例,即您的主MongoDB服务器连接详细信息。$typeMap变量是 Aggregate() 构造函数中的可选参数,用于表示 BSON 反序列化的类型映射。这将应用于返回的游标。


如果您使用的是旧版驱动程序,请考虑运行以下操作,该操作使用 MongoDB 提供的批量操作 API>= 2.6 <= 3.0:

$client = new MongoClient();
$collection = $client->selectCollection("database", "collection");
$pipeline = array(         
    array(
        "$project" => array(
            'data1' => 1,
            "data2" => array("$concat" => array( "$data1", " ", "$data2" ))
        )
    )
);
$batch = new MongoUpdateBatch($collection);
$counter = 0;
foreach ($collection->aggregate($pipeline) as $doc ) {
    $batch->add(
        array(
            "q" => array( '_id' => $doc['_id'] ),
            "u" => array( 
                '$set' => array(
                    "data2" => $doc["data2"]
                )
            )
        )
    );
    $counter++;
    if ($counter % 500 === 0) {
        $retval = $batch->execute(array( 'w' => 1));
        $counter++;
        $batch = new MongoUpdateBatch($collection);        
    }
}
if ($counter > 0) {
    $retval = $batch->execute(array( 'w' => 1 ));
}