gaillard / gridfs-update

一个用于在Mongo GridFS中实现原地更新的库

v1.0.0 2014-07-29 18:18 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:38:02 UTC


README

Build Status

一个用于在Mongo GridFS中实现原地更新的库

要求

需要 PHP 5.4.0(或更高版本)。

安装

要作为本地、项目特定的依赖项添加库,请使用 Composer

{
    "require": {
        "gaillard/gridfs-update": "~1.0"
    }
}

示例

$id = new \MongoId();

$gridfs = (new \MongoClient())->selectDB('gridfsUpdaterExample')->getGridFS();
$gridfs->storeBytes('123456', ['_id' => $id, 'metadata' => ['key1' => 'Hello', 'key2' => 'Mr.', 'key3' => 'Smith']]);

$before = $gridfs->findOne();
echo 'metadata is ';
var_dump($before->file['metadata']);
echo "bytes are {$before->getBytes()}\n";

GridFsUpdater::update(
    $gridfs,
    $id,
    '7890',
    [
        '$set' => ['metadata.key2' => 'Bob'],
        '$unset' => ['metadata.key3' => ''],
    ]
);

$after = $gridfs->findOne();
echo 'metadata is now ';
var_dump($after->file['metadata']);
echo "bytes are now {$after->getBytes()}\n";

打印

metadata is array(3) {
  'key1' =>
  string(5) "Hello"
  'key2' =>
  string(3) "Mr."
  'key3' =>
  string(5) "Smith"
}
bytes are 123456
metadata is now array(2) {
  'key1' =>
  string(5) "Hello"
  'key2' =>
  string(3) "Bob"
}
bytes are now 7890

更新了块而没有删除它们(除非有额外),以及文件doc。主要优点是速度。请留意系统并发访问的任何副作用。mongo-lock-php 库可能有所帮助!

贡献

如果您想贡献,请使用构建过程进行任何更改,并在构建通过后,在github上发送pull请求!

./build.php