88use Illuminate \Database \Query \Expression ;
99use Illuminate \Support \Arr ;
1010use Illuminate \Support \Collection ;
11+ use Illuminate \Support \LazyCollection ;
1112use Illuminate \Support \Str ;
1213use Jenssegers \Mongodb \Connection ;
1314use MongoDB \BSON \Binary ;
1415use MongoDB \BSON \ObjectID ;
1516use MongoDB \BSON \Regex ;
1617use MongoDB \BSON \UTCDateTime ;
18+ use RuntimeException ;
1719
20+ /**
21+ * Class Builder
22+ * @package Jenssegers\Mongodb\Query
23+ */
1824class Builder extends BaseBuilder
1925{
2026 /**
@@ -208,12 +214,25 @@ public function get($columns = [])
208214 return $ this ->getFresh ($ columns );
209215 }
210216
217+ /**
218+ * @inheritdoc
219+ */
220+ public function cursor ($ columns = [])
221+ {
222+ $ result = $ this ->getFresh ($ columns , true );
223+ if ($ result instanceof LazyCollection) {
224+ return $ result ;
225+ }
226+ throw new RuntimeException ("Query not compatible with cursor " );
227+ }
228+
211229 /**
212230 * Execute the query as a fresh "select" statement.
213231 * @param array $columns
214- * @return array|static[]|Collection
232+ * @param bool $returnLazy
233+ * @return array|static[]|Collection|LazyCollection
215234 */
216- public function getFresh ($ columns = [])
235+ public function getFresh ($ columns = [], $ returnLazy = false )
217236 {
218237 // If no columns have been specified for the select statement, we will set them
219238 // here to either the passed columns, or the standard default of retrieving
@@ -401,6 +420,14 @@ public function getFresh($columns = [])
401420 // Execute query and get MongoCursor
402421 $ cursor = $ this ->collection ->find ($ wheres , $ options );
403422
423+ if ($ returnLazy ) {
424+ return LazyCollection::make (function () use ($ cursor ) {
425+ foreach ($ cursor as $ item ) {
426+ yield $ item ;
427+ }
428+ });
429+ }
430+
404431 // Return results as an array with numeric keys
405432 $ results = iterator_to_array ($ cursor , false );
406433 return $ this ->useCollections ? new Collection ($ results ) : $ results ;
@@ -988,6 +1015,7 @@ protected function compileWhereAll(array $where)
9881015 protected function compileWhereBasic (array $ where )
9891016 {
9901017 extract ($ where );
1018+ $ is_numeric = false ;
9911019
9921020 // Replace like or not like with a Regex instance.
9931021 if (in_array ($ operator , ['like ' , 'not like ' ])) {
@@ -999,15 +1027,21 @@ protected function compileWhereBasic(array $where)
9991027
10001028 // Convert to regular expression.
10011029 $ regex = preg_replace ('#(^|[^ \\\])%# ' , '$1.* ' , preg_quote ($ value ));
1030+ $ plain_value = $ value ;
10021031
10031032 // Convert like to regular expression.
10041033 if (!Str::startsWith ($ value , '% ' )) {
10051034 $ regex = '^ ' . $ regex ;
1035+ } else {
1036+ $ plain_value = Str::replaceFirst ('% ' , null , $ plain_value );
10061037 }
10071038 if (!Str::endsWith ($ value , '% ' )) {
10081039 $ regex .= '$ ' ;
1040+ } else {
1041+ $ plain_value = Str::replaceLast ('% ' , null , $ plain_value );
10091042 }
10101043
1044+ $ is_numeric = is_numeric ($ plain_value );
10111045 $ value = new Regex ($ regex , 'i ' );
10121046 } // Manipulate regexp operations.
10131047 elseif (in_array ($ operator , ['regexp ' , 'not regexp ' , 'regex ' , 'not regex ' ])) {
@@ -1027,7 +1061,11 @@ protected function compileWhereBasic(array $where)
10271061 }
10281062
10291063 if (!isset ($ operator ) || $ operator == '= ' ) {
1030- $ query = [$ column => $ value ];
1064+ if ($ is_numeric ) {
1065+ $ query = ['$where ' => '/^ ' .$ value ->getPattern ().'/.test(this. ' .$ column .') ' ];
1066+ } else {
1067+ $ query = [$ column => $ value ];
1068+ }
10311069 } elseif (array_key_exists ($ operator , $ this ->conversion )) {
10321070 $ query = [$ column => [$ this ->conversion [$ operator ] => $ value ]];
10331071 } else {
0 commit comments