district5/mondoc-builder

一个简单易用的面向对象库,用于生成在MongoDB中使用的查询

2.1.0 2024-07-05 07:13 UTC

This package is auto-updated.

Last update: 2024-09-05 08:03:18 UTC


README

CI

MongoDB查询构建库。

安装...

composer require district5/mondoc-builder

测试和修复...

  • 安装依赖项
    $ composer install
    
  • 运行PHPUnit
    $ ./vendor/bin/phpunit
    

构建查询...

该库的核心功能是构建可用于Mongo的查询。

获取QueryBuilder实例

use District5\MondocBuilder\QueryBuilder;

$builder = QueryBuilder::get(); // or $builder = new \District5\MondocBuilder\QueryBuilder();

将选项分配给QueryBuilder(跳过 / 限制 / 排序和自定义选项)

<?php
use District5\MondocBuilder\QueryBuilder;

$builder = QueryBuilder::get();
$options = $builder->getOptions();
// Add some parts
// ...
// ...
$options->setLimit(10); // retrieve 10 document
$options->setSkip(10); // skip the first 10 documents
$options->setSortBy(
    ['$score' => ['$meta' => 'textScore']]
); // sort by name ascending.
// Or you can use setSortBySimple for simple sorting. `$options->setSortBySimple('name', 1);`
$options->setCustom(
    [
        'customOpt1' => 'value',
        'customOpt2' => 'value'
    ]
);

// retrieve the final options array
$optionsForQuery = $options->getArrayCopy();

QueryBuilder包含以下部分...

<?php
use District5\MondocBuilder\QueryBuilder;
use District5\MondocBuilder\QueryTypes\ValueEqualTo;
use District5\MondocBuilder\QueryTypes\ValueNotEqualTo;

$builder = QueryBuilder::get();

$builder->addQueryPart(
    ValueEqualTo::get()->string('name', 'Joe')
);
$builder->addQueryPart(
    ValueNotEqualTo::get()->string('age', 18)
);

存在多种Part类型。以下列出如下

  • AndOperator - $and - 向此对象添加构建器以创建$and查询。
  • OrOperator - $or - 向此对象添加构建器以创建$or查询。
  • KeyExists - $exists - 字段存在或不存在。
  • ValueEqualTo - $eq - 字段等于某个值。
  • ValueGreaterThan - $gt - 字段值大于提供的值。
  • ValueGreaterThanOrEqualTo - $gte - 字段值大于等于给定的值。
  • ValueInValues - $in - 字段值在一个值列表中。
  • ValueNotInValues - $nin - 字段值在一个值列表中。
  • ValueLessThan - $lt - 字段值小于提供的值。
  • ValueLessThanOrEqualTo - $lte - 字段值小于等于提供的值。
  • ValueNotEqualTo - $ne - 字段值不等于提供的值。
  • GeospatialPointNear - $near - 地理空间搜索。
  • GeospatialPointNearSphere - $nearSphere - 地理空间搜索。
  • HasAllValues - $all - 字段值包含列表中的所有项。
  • SizeOfValue - $size - 值的大小等于此值。

使用AndOperator$and查询

<?php
use District5\MondocBuilder\QueryBuilder;
use District5\MondocBuilder\QueryTypes\AndOperator;

$and = new AndOperator();

$andBuilderOne = QueryBuilder::get();
// add parts
// ...
$andBuilderTwo = QueryBuilder::get();
// add parts
// ...

$and->addBuilder(
    $andBuilderOne
)->addBuilder(
    $andBuilderTwo
);

$builder = QueryBuilder::get();
$builder->addQueryPart($and);

$query = $builder->getArrayCopy();
$options = $builder->getOptions()->getArrayCopy();

使用OrOperator$or查询

<?php
use District5\MondocBuilder\QueryBuilder;
use District5\MondocBuilder\QueryTypes\OrOperator;

$orBuilderOne = QueryBuilder::get();
// add parts
// ...
$orBuilderTwo = QueryBuilder::get();
// add parts
// ...
$or = new OrOperator();
$or->addBuilder(
    $orBuilderOne
)->addBuilder(
    $orBuilderTwo
);
$builder = QueryBuilder::get();
$builder->addQueryPart($or);

$query = $builder->getArrayCopy();
$options = $builder->getOptions()->getArrayCopy();

简单的等于示例...

<?php
use District5\MondocBuilder\QueryBuilder;
use District5\MondocBuilder\QueryTypes\ValueNotEqualTo;

$builder = QueryBuilder::get();
// or new QueryBuilder();

// add a part
$part = new ValueNotEqualTo();
$part->string('name', 'Joe');
$builder->addQueryPart($part);

// you can inspect or get the final database query with:
$query = $builder->getArrayCopy();
// you can inspect or get the final database options part with:
$options = $builder->getOptions()->getArrayCopy();

// use $query and $options in the query on Mongo.

更深入示例...

<?php
use District5\MondocBuilder\QueryBuilder;
use District5\MondocBuilder\QueryTypes\GeospatialPointNear;use District5\MondocBuilder\QueryTypes\GeospatialPointNearSphere;use District5\MondocBuilder\QueryTypes\HasAllValues;use District5\MondocBuilder\QueryTypes\SizeOfValue;use District5\MondocBuilder\QueryTypes\ValueEqualTo;
use District5\MondocBuilder\QueryTypes\ValueGreaterThan;use District5\MondocBuilder\QueryTypes\ValueGreaterThanOrEqualTo;use District5\MondocBuilder\QueryTypes\ValueInValues;use District5\MondocBuilder\QueryTypes\ValueLessThan;use District5\MondocBuilder\QueryTypes\ValueLessThanOrEqualTo;use District5\MondocBuilder\QueryTypes\ValueNotEqualTo;use District5\MondocBuilder\QueryTypes\ValueNotInValues;use MongoDB\BSON\ObjectId;
use MongoDB\BSON\UTCDateTime;

$builder = QueryBuilder::get();

// '$exists' query part
$exists = new KeyExists();
$exists->true('aField');

// basic equals
$equals = new ValueEqualTo();
$equals->string('aField', 'Joe');
$equals->integer('aField', 1);
$equals->boolean('aField', true);
$equals->dateTime('aField', new DateTime());
$equals->float('aField', 1.234);
$equals->double('aField', 1.234);
$equals->null('aField');

$equals->objectId('_id', new ObjectId()); // or you can use ->mongoNative
$equals->objectId('accountId', new ObjectId()); // or you can use ->mongoNative
$equals->mongoNative('aDate', new UTCDateTime());
$builder->addQueryPart($equals);

// basic NOT equal to
// @todo inherits the same methods as the 'ValueEqualTo' part documented above.
$notEqual = new ValueNotEqualTo();
$notEqual->integer('aField', 1);
$builder->addQueryPart($notEqual);

// $size queries
$size = new SizeOfValue();
$size->equals('aField', 2);
$builder->addQueryPart($size);

// greater than
// @todo inherits the same methods as the 'ValueEqualTo' part documented above.
$greaterThan = new ValueGreaterThan();
$greaterThan->integer('aField', 1);
$builder->addQueryPart($greaterThan);

// greater than or equal to
// @todo inherits the same methods as the 'ValueEqualTo' part documented above.
$greaterThanOrEqualTo = new ValueGreaterThanOrEqualTo();
$greaterThanOrEqualTo->integer('aField', 1);
$builder->addQueryPart($greaterThanOrEqualTo);

// less than
// @todo inherits the same methods as the 'ValueEqualTo' part documented above.
$lessThan = new ValueLessThan();
$lessThan->integer('aField', 1);
$builder->addQueryPart($lessThan);

// less than or equal to
// @todo inherits the same methods as the 'ValueEqualTo' part documented above.
$lessThanOrEqualTo = new ValueLessThanOrEqualTo();
$lessThanOrEqualTo->integer('aField', 1);
$builder->addQueryPart($lessThanOrEqualTo);

// value in queries - $in
$in = new ValueInValues();
$in->anArray('aField', ['foo', 'bar', 'dog', 'cat']);
$builder->addQueryPart($in);

// not in queries - $nin
$nin = new ValueNotInValues();
$nin->anArray('aField', ['foo', 'bar', 'dog', 'cat']);
$builder->addQueryPart($nin);

// geospatial query part - $nearSphere
$geo = new GeospatialPointNearSphere();
$geo->withinXMetresOfCoordinates('myLocation', 40, -0.1415681, 51.5006761); // Within 40 metres of Buckingham Palace
$geo->withinXMilesOfCoordinates('myLocation', 1, -0.1415681, 51.5006761); // Within 1 mile of Buckingham Palace
$builder->addQueryPart($geo);

// geospatial query part - $near
$geo = new GeospatialPointNear();
$geo->withinXMetresOfCoordinates('myLocation', 40, -0.1415681, 51.5006761); // Within 40 metres of Buckingham Palace
$geo->withinXMilesOfCoordinates('myLocation', 1, -0.1415681, 51.5006761); // Within 1 mile of Buckingham Palace
$builder->addQueryPart($geo);

// has all values
$hasAll = new HasAllValues();
$hasAll->anArray('field', ['my', 'possible', 'values']);
$builder->addQueryPart($hasAll);

// Custom query parts
$builder->addCustomArrayPart(
    [
        'aField' => 'FooBar',
        'anotherField' => 'Joe Bloggs'
    ]
);
$builder->addCustomArrayPart(
    [
        'andAnotherField' => 'ABC',
        'aFinalField' => 123
    ]
);

// you can inspect or get the final database query with:
$query = $builder->getArrayCopy();
// you can inspect or get the final database options part with:
$options = $builder->getOptions()->getArrayCopy();

// use $query and $options in the query on Mongo.