@@ -36,11 +36,13 @@ public function insertOneAction() : Response {
3636 new \MongoDB \BSON \ObjectId ($ decodedRequestBody ['document ' ]['_id ' ]);
3737 }
3838
39- foreach ($ decodedRequestBody ['document ' ] as &$ insertValue ) {
39+ array_walk_recursive ($ decodedRequestBody ['document ' ], function (&$ insertValue ) {
40+
4041 if ( preg_match (MongoDBHelper::ISO_DATE_TIME_REGEX , $ insertValue ) ) {
4142 $ insertValue = new \MongoDB \BSON \UTCDateTime (new \DateTime ($ insertValue ));
4243 }
43- }
44+
45+ });
4446
4547 try {
4648
@@ -155,14 +157,20 @@ public function findAction() : Response {
155157 return new JsonResponse (500 , ErrorNormalizer::normalize ($ th , __METHOD__ ));
156158 }
157159
158- foreach ($ documents as $ document ) {
159- foreach ($ document as &$ documentValue ) {
160+ foreach ($ documents as &$ document ) {
161+
162+ $ document = $ document ->jsonSerialize ();
163+
164+ array_walk_recursive ($ document , function (&$ documentValue ) {
165+
160166 if ( is_a ($ documentValue , '\MongoDB\BSON\ObjectId ' ) ) {
161167 $ documentValue = (string ) $ documentValue ;
162168 } elseif ( is_a ($ documentValue , '\MongoDB\BSON\UTCDatetime ' ) ) {
163169 $ documentValue = $ documentValue ->toDateTime ()->format ('Y-m-d\TH:i:s.v\Z ' );
164170 }
165- }
171+
172+ });
173+
166174 }
167175
168176 return new JsonResponse (200 , $ documents );
@@ -234,7 +242,19 @@ public function enumFieldsAction() : Response {
234242 return new JsonResponse (200 , []);
235243 }
236244
237- $ array = json_decode (json_encode ($ documents [0 ]), JSON_OBJECT_AS_ARRAY );
245+ $ document = $ documents [0 ]->jsonSerialize ();
246+
247+ array_walk_recursive ($ document , function (&$ documentValue ) {
248+
249+ if ( is_a ($ documentValue , '\MongoDB\BSON\ObjectId ' ) ) {
250+ $ documentValue = (string ) $ documentValue ;
251+ } elseif ( is_a ($ documentValue , '\MongoDB\BSON\UTCDatetime ' ) ) {
252+ $ documentValue = $ documentValue ->toDateTime ()->format ('Y-m-d\TH:i:s.v\Z ' );
253+ }
254+
255+ });
256+
257+ $ array = json_decode (json_encode ($ document ), JSON_OBJECT_AS_ARRAY );
238258
239259 /**
240260 * Converts multidimensional array to 2D array with dot notation keys.
@@ -253,10 +273,7 @@ public function enumFieldsAction() : Response {
253273
254274 $ documentFields = array_unique ($ result );
255275
256- // We ignore $oid since it represents a \MongoDB\BSON\ObjectId object.
257- $ fixedDocumentFields = str_replace ('_id.$oid ' , '_id ' , json_encode ($ documentFields ));
258-
259- return new JsonResponse (200 , json_decode ($ fixedDocumentFields ));
276+ return new JsonResponse (200 , $ documentFields );
260277
261278 }
262279
0 commit comments