@@ -73,6 +73,50 @@ static inline bool php_phongo_bulkwrite_update_has_operators(bson_t* bupdate) /*
7373 return false;
7474} /* }}} */
7575
76+ /* Returns whether the update document is considered an aggregation pipeline */
77+ static inline bool php_phongo_bulkwrite_update_is_pipeline (bson_t * bupdate ) /* {{{ */
78+ {
79+ bson_iter_t iter ;
80+ bson_iter_t child ;
81+ const char * key ;
82+ int i = 0 ;
83+ char * i_str ;
84+
85+ if (!bson_iter_init (& iter , bupdate )) {
86+ return false;
87+ }
88+
89+ while (bson_iter_next (& iter )) {
90+ key = bson_iter_key (& iter );
91+ i_str = bson_strdup_printf ("%d" , i ++ );
92+
93+ if (strcmp (key , i_str )) {
94+ bson_free (i_str );
95+ return false;
96+ }
97+
98+ bson_free (i_str );
99+
100+ if (BSON_ITER_HOLDS_DOCUMENT (& iter )) {
101+ if (!bson_iter_recurse (& iter , & child )) {
102+ return false;
103+ }
104+ if (!bson_iter_next (& child )) {
105+ return false;
106+ }
107+ key = bson_iter_key (& child );
108+ if (key [0 ] != '$' ) {
109+ return false;
110+ }
111+ } else {
112+ return false;
113+ }
114+ }
115+
116+ /* should return false when the document is empty */
117+ return i != 0 ;
118+ } /* }}} */
119+
76120/* Returns whether the BSON array's keys are a sequence of integer strings
77121 * starting with "0". BSON_APPEND_ARRAY considers it the caller's responsibility
78122 * to ensure that the array's keys are properly formatted. */
@@ -345,7 +389,7 @@ static PHP_METHOD(BulkWrite, update)
345389 goto cleanup ;
346390 }
347391
348- if (php_phongo_bulkwrite_update_has_operators (& bupdate )) {
392+ if (php_phongo_bulkwrite_update_has_operators (& bupdate ) || php_phongo_bulkwrite_update_is_pipeline ( & bupdate ) ) {
349393 if (zoptions && php_array_existsc (zoptions , "multi" ) && php_array_fetchc_bool (zoptions , "multi" )) {
350394 if (!mongoc_bulk_operation_update_many_with_opts (intern -> bulk , & bquery , & bupdate , & boptions , & error )) {
351395 phongo_throw_exception_from_bson_error_t (& error TSRMLS_CC );
0 commit comments