-
-
Notifications
You must be signed in to change notification settings - Fork 509
/
Copy pathExpr.php
1466 lines (1281 loc) · 41.4 KB
/
Expr.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
namespace Doctrine\ODM\MongoDB\Query;
use BadMethodCallException;
use Doctrine\ODM\MongoDB\Aggregation;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Mapping\MappingException;
use GeoJson\Geometry\Geometry;
use GeoJson\Geometry\Point;
use InvalidArgumentException;
use LogicException;
use MongoDB\BSON\Binary;
use MongoDB\BSON\Javascript;
use function array_key_exists;
use function array_map;
use function array_merge;
use function array_values;
use function assert;
use function explode;
use function func_get_args;
use function in_array;
use function is_array;
use function is_string;
use function key;
use function sprintf;
use function strpos;
use function strtolower;
/**
* Query expression builder for ODM.
*/
class Expr
{
/**
* The query criteria array.
*
* @var array
*/
private $query = [];
/**
* The "new object" array containing either a full document or a number of
* atomic update operators.
*
* @see docs.mongodb.org/manual/reference/method/db.collection.update/#update-parameter
*
* @var array
*/
private $newObj = [];
/**
* The current field we are operating on.
*
* @var string|null
*/
private $currentField;
/**
* The DocumentManager instance for this query
*
* @var DocumentManager
*/
private $dm;
/**
* The ClassMetadata instance for the document being queried
*
* @var ClassMetadata
*/
private $class;
public function __construct(DocumentManager $dm)
{
$this->dm = $dm;
}
/**
* Add one or more $and clauses to the current query.
*
* @see Builder::addAnd()
* @see https://docs.mongodb.com/manual/reference/operator/and/
*
* @param array|Expr $expression
* @param array|Expr ...$expressions
*/
public function addAnd($expression, ...$expressions): self
{
if (! isset($this->query['$and'])) {
$this->query['$and'] = [];
}
$this->query['$and'] = array_merge(
$this->query['$and'],
func_get_args()
);
return $this;
}
/**
* Add one or more $nor clauses to the current query.
*
* @see Builder::addNor()
* @see https://docs.mongodb.com/manual/reference/operator/nor/
*
* @param array|Expr $expression
* @param array|Expr ...$expressions
*/
public function addNor($expression, ...$expressions): self
{
if (! isset($this->query['$nor'])) {
$this->query['$nor'] = [];
}
$this->query['$nor'] = array_merge(
$this->query['$nor'],
func_get_args()
);
return $this;
}
/**
* Add one or more $or clauses to the current query.
*
* @see Builder::addOr()
* @see https://docs.mongodb.com/manual/reference/operator/or/
*
* @param array|Expr $expression
* @param array|Expr ...$expressions
*/
public function addOr($expression, ...$expressions): self
{
if (! isset($this->query['$or'])) {
$this->query['$or'] = [];
}
$this->query['$or'] = array_merge(
$this->query['$or'],
func_get_args()
);
return $this;
}
/**
* Append one or more values to the current array field only if they do not
* already exist in the array.
*
* If the field does not exist, it will be set to an array containing the
* unique value(s) in the argument. If the field is not an array, the query
* will yield an error.
*
* Multiple values may be specified by provided an Expr object and using
* {@link Expr::each()}.
*
* @see Builder::addToSet()
* @see https://docs.mongodb.com/manual/reference/operator/addToSet/
* @see https://docs.mongodb.com/manual/reference/operator/each/
*
* @param mixed|Expr $valueOrExpression
*/
public function addToSet($valueOrExpression): self
{
$this->requiresCurrentField();
$this->newObj['$addToSet'][$this->currentField] = static::convertExpression($valueOrExpression, $this->class);
return $this;
}
/**
* Specify $all criteria for the current field.
*
* @see Builder::all()
* @see https://docs.mongodb.com/manual/reference/operator/all/
*/
public function all(array $values): self
{
return $this->operator('$all', $values);
}
/**
* Apply a bitwise operation on the current field
*
* @see https://docs.mongodb.com/manual/reference/operator/update/bit/
*/
protected function bit(string $operator, int $value): self
{
$this->requiresCurrentField();
$this->newObj['$bit'][$this->currentField][$operator] = $value;
return $this;
}
/**
* Apply a bitwise and operation on the current field.
*
* @see Builder::bitAnd()
* @see https://docs.mongodb.com/manual/reference/operator/update/bit/
*/
public function bitAnd(int $value): self
{
return $this->bit('and', $value);
}
/**
* Apply a bitwise or operation on the current field.
*
* @see Builder::bitOr()
* @see https://docs.mongodb.com/manual/reference/operator/update/bit/
*/
public function bitOr(int $value): self
{
return $this->bit('or', $value);
}
/**
* Matches documents where all of the bit positions given by the query are
* clear.
*
* @see Builder::bitsAllClear()
* @see https://docs.mongodb.com/manual/reference/operator/query/bitsAllClear/
*
* @param int|array|Binary $value
*/
public function bitsAllClear($value): self
{
$this->requiresCurrentField();
return $this->operator('$bitsAllClear', $value);
}
/**
* Matches documents where all of the bit positions given by the query are
* set.
*
* @see Builder::bitsAllSet()
* @see https://docs.mongodb.com/manual/reference/operator/query/bitsAllSet/
*
* @param int|array|Binary $value
*/
public function bitsAllSet($value): self
{
$this->requiresCurrentField();
return $this->operator('$bitsAllSet', $value);
}
/**
* Matches documents where any of the bit positions given by the query are
* clear.
*
* @see Builder::bitsAnyClear()
* @see https://docs.mongodb.com/manual/reference/operator/query/bitsAnyClear/
*
* @param int|array|Binary $value
*/
public function bitsAnyClear($value): self
{
$this->requiresCurrentField();
return $this->operator('$bitsAnyClear', $value);
}
/**
* Matches documents where any of the bit positions given by the query are
* set.
*
* @see Builder::bitsAnySet()
* @see https://docs.mongodb.com/manual/reference/operator/query/bitsAnySet/
*
* @param int|array|Binary $value
*/
public function bitsAnySet($value): self
{
$this->requiresCurrentField();
return $this->operator('$bitsAnySet', $value);
}
/**
* Apply a bitwise xor operation on the current field.
*
* @see Builder::bitXor()
* @see https://docs.mongodb.com/manual/reference/operator/update/bit/
*/
public function bitXor(int $value): self
{
return $this->bit('xor', $value);
}
/**
* A boolean flag to enable or disable case sensitive search for $text
* criteria.
*
* This method must be called after text().
*
* @see Builder::caseSensitive()
* @see https://docs.mongodb.com/manual/reference/operator/query/text/
*
* @throws BadMethodCallException If the query does not already have $text criteria.
*/
public function caseSensitive(bool $caseSensitive): self
{
if (! isset($this->query['$text'])) {
throw new BadMethodCallException('This method requires a $text operator (call text() first)');
}
// Remove caseSensitive option to keep support for older database versions
if ($caseSensitive) {
$this->query['$text']['$caseSensitive'] = true;
} elseif (isset($this->query['$text']['$caseSensitive'])) {
unset($this->query['$text']['$caseSensitive']);
}
return $this;
}
/**
* Associates a comment to any expression taking a query predicate.
*
* @see Builder::comment()
* @see https://docs.mongodb.com/manual/reference/operator/query/comment/
*/
public function comment(string $comment): self
{
$this->query['$comment'] = $comment;
return $this;
}
/**
* Sets the value of the current field to the current date, either as a date or a timestamp.
*
* @see Builder::currentDate()
* @see https://docs.mongodb.com/manual/reference/operator/update/currentDate/
*
* @throws InvalidArgumentException If an invalid type is given.
*/
public function currentDate(string $type = 'date'): self
{
if (! in_array($type, ['date', 'timestamp'])) {
throw new InvalidArgumentException('Type for currentDate operator must be date or timestamp.');
}
$this->requiresCurrentField();
$this->newObj['$currentDate'][$this->currentField]['$type'] = $type;
return $this;
}
/**
* A boolean flag to enable or disable diacritic sensitive search for $text
* criteria.
*
* This method must be called after text().
*
* @see Builder::diacriticSensitive()
* @see https://docs.mongodb.com/manual/reference/operator/query/text/
*
* @throws BadMethodCallException If the query does not already have $text criteria.
*/
public function diacriticSensitive(bool $diacriticSensitive): self
{
if (! isset($this->query['$text'])) {
throw new BadMethodCallException('This method requires a $text operator (call text() first)');
}
// Remove diacriticSensitive option to keep support for older database versions
if ($diacriticSensitive) {
$this->query['$text']['$diacriticSensitive'] = true;
} elseif (isset($this->query['$text']['$diacriticSensitive'])) {
unset($this->query['$text']['$diacriticSensitive']);
}
return $this;
}
/**
* Add $each criteria to the expression for a $push operation.
*
* @see Expr::push()
* @see https://docs.mongodb.com/manual/reference/operator/each/
*/
public function each(array $values): self
{
return $this->operator('$each', $values);
}
/**
* Specify $elemMatch criteria for the current field.
*
* @see Builder::elemMatch()
* @see https://docs.mongodb.com/manual/reference/operator/elemMatch/
*
* @param array|Expr $expression
*/
public function elemMatch($expression): self
{
return $this->operator('$elemMatch', $expression);
}
/**
* Specify $expr criteria for the current field.
*
* @see Builder::aggregationExpression()
* @see https://docs.mongodb.com/manual/reference/operator/query/expr/
*
* @param array|Aggregation\Expr $expression
*/
public function aggregationExpression($expression): self
{
return $this->operator('$expr', $expression);
}
/**
* Specify an equality match for the current field.
*
* @see Builder::equals()
*
* @param mixed $value
*/
public function equals($value): self
{
if ($this->currentField) {
$this->query[$this->currentField] = $value;
} else {
$this->query = $value;
}
return $this;
}
/**
* Specify $exists criteria for the current field.
*
* @see Builder::exists()
* @see https://docs.mongodb.com/manual/reference/operator/exists/
*/
public function exists(bool $bool): self
{
return $this->operator('$exists', $bool);
}
/**
* Set the current field for building the expression.
*
* @see Builder::field()
*/
public function field(string $field): self
{
$this->currentField = $field;
return $this;
}
/**
* Add $geoIntersects criteria with a GeoJSON geometry to the expression.
*
* The geometry parameter GeoJSON object or an array corresponding to the
* geometry's JSON representation.
*
* @see Builder::geoIntersects()
* @see https://docs.mongodb.com/manual/reference/operator/geoIntersects/
*
* @param array|Geometry $geometry
*/
public function geoIntersects($geometry): self
{
if ($geometry instanceof Geometry) {
$geometry = $geometry->jsonSerialize();
}
return $this->operator('$geoIntersects', ['$geometry' => $geometry]);
}
/**
* Add $geoWithin criteria with a GeoJSON geometry to the expression.
*
* The geometry parameter GeoJSON object or an array corresponding to the
* geometry's JSON representation.
*
* @see Builder::geoWithin()
* @see https://docs.mongodb.com/manual/reference/operator/geoIntersects/
*
* @param array|Geometry $geometry
*/
public function geoWithin($geometry): self
{
if ($geometry instanceof Geometry) {
$geometry = $geometry->jsonSerialize();
}
return $this->operator('$geoWithin', ['$geometry' => $geometry]);
}
/**
* Add $geoWithin criteria with a $box shape to the expression.
*
* A rectangular polygon will be constructed from a pair of coordinates
* corresponding to the bottom left and top right corners.
*
* Note: the $box operator only supports legacy coordinate pairs and 2d
* indexes. This cannot be used with 2dsphere indexes and GeoJSON shapes.
*
* @see Builder::geoWithinBox()
* @see https://docs.mongodb.com/manual/reference/operator/box/
*/
public function geoWithinBox(float $x1, float $y1, float $x2, float $y2): self
{
$shape = ['$box' => [[$x1, $y1], [$x2, $y2]]];
return $this->operator('$geoWithin', $shape);
}
/**
* Add $geoWithin criteria with a $center shape to the expression.
*
* Note: the $center operator only supports legacy coordinate pairs and 2d
* indexes. This cannot be used with 2dsphere indexes and GeoJSON shapes.
*
* @see Builider::geoWithinCenter()
* @see https://docs.mongodb.com/manual/reference/operator/center/
*/
public function geoWithinCenter(float $x, float $y, float $radius): self
{
$shape = ['$center' => [[$x, $y], $radius]];
return $this->operator('$geoWithin', $shape);
}
/**
* Add $geoWithin criteria with a $centerSphere shape to the expression.
*
* Note: the $centerSphere operator supports both 2d and 2dsphere indexes.
*
* @see Builder::geoWithinCenterSphere()
* @see https://docs.mongodb.com/manual/reference/operator/centerSphere/
*/
public function geoWithinCenterSphere(float $x, float $y, float $radius): self
{
$shape = ['$centerSphere' => [[$x, $y], $radius]];
return $this->operator('$geoWithin', $shape);
}
/**
* Add $geoWithin criteria with a $polygon shape to the expression.
*
* Point coordinates are in x, y order (easting, northing for projected
* coordinates, longitude, latitude for geographic coordinates).
*
* The last point coordinate is implicitly connected with the first.
*
* Note: the $polygon operator only supports legacy coordinate pairs and 2d
* indexes. This cannot be used with 2dsphere indexes and GeoJSON shapes.
*
* @see Builder::geoWithinPolygon()
* @see https://docs.mongodb.com/manual/reference/operator/polygon/
*
* @param array $point1 First point of the polygon
* @param array $point2 Second point of the polygon
* @param array $point3 Third point of the polygon
* @param array ...$points Additional points of the polygon
*
* @throws InvalidArgumentException If less than three points are given.
*/
public function geoWithinPolygon($point1, $point2, $point3, ...$points): self
{
$shape = ['$polygon' => func_get_args()];
return $this->operator('$geoWithin', $shape);
}
/**
* Return the current field.
*/
public function getCurrentField(): ?string
{
return $this->currentField;
}
/**
* Gets prepared newObj part of expression.
*/
public function getNewObj(): array
{
return $this->dm->getUnitOfWork()
->getDocumentPersister($this->class->name)
->prepareQueryOrNewObj($this->newObj, true);
}
/**
* Gets prepared query part of expression.
*/
public function getQuery(): array
{
return $this->dm->getUnitOfWork()
->getDocumentPersister($this->class->name)
->prepareQueryOrNewObj($this->convertExpressions($this->query));
}
/**
* Specify $gt criteria for the current field.
*
* @see Builder::gt()
* @see https://docs.mongodb.com/manual/reference/operator/gt/
*
* @param mixed $value
*/
public function gt($value): self
{
return $this->operator('$gt', $value);
}
/**
* Specify $gte criteria for the current field.
*
* @see Builder::gte()
* @see https://docs.mongodb.com/manual/reference/operator/gte/
*
* @param mixed $value
*/
public function gte($value): self
{
return $this->operator('$gte', $value);
}
/**
* Specify $in criteria for the current field.
*
* @see Builder::in()
* @see https://docs.mongodb.com/manual/reference/operator/in/
*/
public function in(array $values): self
{
return $this->operator('$in', array_values($values));
}
/**
* Increment the current field.
*
* If the field does not exist, it will be set to this value.
*
* @see Builder::inc()
* @see https://docs.mongodb.com/manual/reference/operator/inc/
*
* @param float|int $value
*/
public function inc($value): self
{
$this->requiresCurrentField();
$this->newObj['$inc'][$this->currentField] = $value;
return $this;
}
/**
* Checks that the current field includes a reference to the supplied document.
*/
public function includesReferenceTo(object $document): self
{
$this->requiresCurrentField();
$mapping = $this->getReferenceMapping();
$reference = $this->dm->createReference($document, $mapping);
$storeAs = $mapping['storeAs'] ?? null;
$keys = [];
switch ($storeAs) {
case ClassMetadata::REFERENCE_STORE_AS_ID:
$this->query[$mapping['name']] = $reference;
return $this;
case ClassMetadata::REFERENCE_STORE_AS_REF:
$keys = ['id' => true];
break;
case ClassMetadata::REFERENCE_STORE_AS_DB_REF:
case ClassMetadata::REFERENCE_STORE_AS_DB_REF_WITH_DB:
$keys = ['$ref' => true, '$id' => true, '$db' => true];
if ($storeAs === ClassMetadata::REFERENCE_STORE_AS_DB_REF) {
unset($keys['$db']);
}
if (isset($mapping['targetDocument'])) {
unset($keys['$ref'], $keys['$db']);
}
break;
default:
throw new InvalidArgumentException(sprintf('Reference type %s is invalid.', $storeAs));
}
foreach ($keys as $key => $value) {
$this->query[$mapping['name']]['$elemMatch'][$key] = $reference[$key];
}
return $this;
}
/**
* Set the $language option for $text criteria.
*
* This method must be called after text().
*
* @see Builder::language()
* @see https://docs.mongodb.com/manual/reference/operator/query/text/
*
* @throws BadMethodCallException If the query does not already have $text criteria.
*/
public function language(string $language): self
{
if (! isset($this->query['$text'])) {
throw new BadMethodCallException('This method requires a $text operator (call text() first)');
}
$this->query['$text']['$language'] = $language;
return $this;
}
/**
* Specify $lt criteria for the current field.
*
* @see Builder::lte()
* @see https://docs.mongodb.com/manual/reference/operator/lte/
*
* @param mixed $value
*/
public function lt($value): self
{
return $this->operator('$lt', $value);
}
/**
* Specify $lte criteria for the current field.
*
* @see Builder::lte()
* @see https://docs.mongodb.com/manual/reference/operator/lte/
*
* @param mixed $value
*/
public function lte($value): self
{
return $this->operator('$lte', $value);
}
/**
* Updates the value of the field to a specified value if the specified value is greater than the current value of the field.
*
* @see Builder::max()
* @see https://docs.mongodb.com/manual/reference/operator/update/max/
*
* @param mixed $value
*/
public function max($value): self
{
$this->requiresCurrentField();
$this->newObj['$max'][$this->currentField] = $value;
return $this;
}
/**
* Updates the value of the field to a specified value if the specified value is less than the current value of the field.
*
* @see Builder::min()
* @see https://docs.mongodb.com/manual/reference/operator/update/min/
*
* @param mixed $value
*/
public function min($value): self
{
$this->requiresCurrentField();
$this->newObj['$min'][$this->currentField] = $value;
return $this;
}
/**
* Specify $mod criteria for the current field.
*
* @see Builder::mod()
* @see https://docs.mongodb.com/manual/reference/operator/mod/
*
* @param float|int $divisor
* @param float|int $remainder
*/
public function mod($divisor, $remainder = 0): self
{
return $this->operator('$mod', [$divisor, $remainder]);
}
/**
* Multiply the current field.
*
* If the field does not exist, it will be set to 0.
*
* @see Builder::mul()
* @see https://docs.mongodb.com/manual/reference/operator/update/mul/
*
* @param float|int $value
*/
public function mul($value): self
{
$this->requiresCurrentField();
$this->newObj['$mul'][$this->currentField] = $value;
return $this;
}
/**
* Add $near criteria to the expression.
*
* A GeoJSON point may be provided as the first and only argument for
* 2dsphere queries. This single parameter may be a GeoJSON point object or
* an array corresponding to the point's JSON representation.
*
* @see Builder::near()
* @see https://docs.mongodb.com/manual/reference/operator/near/
*
* @param float|array|Point $x
* @param float $y
*/
public function near($x, $y = null): self
{
if ($x instanceof Point) {
$x = $x->jsonSerialize();
}
if (is_array($x)) {
return $this->operator('$near', ['$geometry' => $x]);
}
return $this->operator('$near', [$x, $y]);
}
/**
* Add $nearSphere criteria to the expression.
*
* A GeoJSON point may be provided as the first and only argument for
* 2dsphere queries. This single parameter may be a GeoJSON point object or
* an array corresponding to the point's JSON representation.
*
* @see Builder::nearSphere()
* @see https://docs.mongodb.com/manual/reference/operator/nearSphere/
*
* @param float|array|Point $x
* @param float $y
*/
public function nearSphere($x, $y = null): self
{
if ($x instanceof Point) {
$x = $x->jsonSerialize();
}
if (is_array($x)) {
return $this->operator('$nearSphere', ['$geometry' => $x]);
}
return $this->operator('$nearSphere', [$x, $y]);
}
/**
* Negates an expression for the current field.
*
* @see Builder::not()
* @see https://docs.mongodb.com/manual/reference/operator/not/
*
* @param array|Expr|mixed $expression
*/
public function not($expression): self
{
return $this->operator('$not', $expression);
}
/**
* Specify $ne criteria for the current field.
*
* @see Builder::notEqual()
* @see https://docs.mongodb.com/manual/reference/operator/ne/
*
* @param mixed $value
*/
public function notEqual($value): self
{
return $this->operator('$ne', $value);
}
/**
* Specify $nin criteria for the current field.
*
* @see Builder::notIn()
* @see https://docs.mongodb.com/manual/reference/operator/nin/
*/
public function notIn(array $values): self
{
return $this->operator('$nin', array_values($values));
}
/**
* Defines an operator and value on the expression.
*
* If there is a current field, the operator will be set on it; otherwise,
* the operator is set at the top level of the query.
*
* @param mixed $value
*/
public function operator(string $operator, $value): self
{
$this->wrapEqualityCriteria();
if ($this->currentField) {
$this->query[$this->currentField][$operator] = $value;
} else {
$this->query[$operator] = $value;
}
return $this;
}
/**
* Remove the first element from the current array field.
*
* @see Builder::popFirst()
* @see https://docs.mongodb.com/manual/reference/operator/pop/
*/
public function popFirst(): self
{
$this->requiresCurrentField();
$this->newObj['$pop'][$this->currentField] = -1;
return $this;
}
/**
* Remove the last element from the current array field.
*
* @see Builder::popLast()
* @see https://docs.mongodb.com/manual/reference/operator/pop/
*/
public function popLast(): self
{
$this->requiresCurrentField();
$this->newObj['$pop'][$this->currentField] = 1;
return $this;
}
/**
* Add $position criteria to the expression for a $push operation.
*
* This is useful in conjunction with {@link Expr::each()} for a
* {@link Expr::push()} operation.
*
* @see https://docs.mongodb.com/manual/reference/operator/update/position/
*/
public function position(int $position): self
{
return $this->operator('$position', $position);
}
/**
* Remove all elements matching the given value or expression from the
* current array field.
*
* @see Builder::pull()
* @see https://docs.mongodb.com/manual/reference/operator/pull/
*
* @param mixed|Expr $valueOrExpression
*/
public function pull($valueOrExpression): self
{
$this->requiresCurrentField();
$this->newObj['$pull'][$this->currentField] = static::convertExpression($valueOrExpression, $this->class);
return $this;
}
/**
* Remove all elements matching any of the given values from the current
* array field.
*
* @see Builder::pullAll()
* @see https://docs.mongodb.com/manual/reference/operator/pullAll/
*/
public function pullAll(array $values): self
{
$this->requiresCurrentField();
$this->newObj['$pullAll'][$this->currentField] = $values;
return $this;