2021年2月4日星期四

Search with PHP inside MongoDB

Im trying many days now to select data from this db [1]: https://i.stack.imgur.com/QA34L.jpg

I want to print for example all comments echo $comment['username']." | "; \ Alex | Alex

My php code so far:

    [![$m= new MongoDB\Client ("mongodb://127.0.0.1/");      $db = $m->stores;      $collection = $db->storeinfo;][1]][1]       $storez = $collection->find(array("Products.pTHUMBNAIL" => $pThumb));      $o=1;      $afm=array();      foreach ($storez as $stor) {          $afm[$o] = $stor['AFM'];            $record = $collection->findOne(array(              "AFM" => $afm[$o],"Products.pTHUMBNAIL" => $pThumb));          foreach ($record['Products'] as $pro){              if($pThumb == $pro['pTHUMBNAIL']){                  echo $temp = $pro['pID']." ";                  foreach($pro as $pro1['pCOMMENTS']) {                      foreach($pro1 as $com['Comment']) {                          var_dump($com['Comment']);                          /*                          foreach($com as $comment) {                              echo $comment['username'];                          }                          */                  }                                }              }          }            $o += 1;      }  

It seems that i just cannot find the correct foreach to loop through my Comment array

var_dump output:

099360111/1 object(MongoDB\BSON\ObjectId)#55 (1) { ["oid"]=> string(24) "6003403a695900000c002649" } string(11) "099360111/1" string(9) "Old Skool" string(2) "75" string(4) "Vans" string(25) "Leather and textile upper" string(2) "44" string(18) "Men/Shoes/Trainers" string(52) "http://127.0.0.1/pricedoc/assets/img/products/p1.jpg" string(1) "7" object(MongoDB\Model\BSONArray)#65 (1) { ["storage":"ArrayObject":private]=> array(1) { [0]=> object(MongoDB\Model\BSONDocument)#10 (1) { ["storage":"ArrayObject":private]=> array(1) { ["Comment"]=> object(MongoDB\Model\BSONDocument)#73 (1) { ["storage":"ArrayObject":private]=> array(4) { ["username"]=> string(4) "Alex" ["date"]=> object(MongoDB\BSON\UTCDateTime)#45 (1) { ["milliseconds"]=> string(13) "1611028053000" } ["text"]=> string(21) "1st comment from user" ["rating"]=> string(1) "4" } } } } } } 099360666/1 object(MongoDB\BSON\ObjectId)#44 (1) { ["oid"]=> string(24) "6006563a3f1c0000c80034a8" } string(11) "099360666/1" string(12) "old school 2" string(2) "50" string(4) "Vans" string(11) "black/white" string(8) "42,43,43" string(18) "Men/Shoes/Trainers" string(52) "http://127.0.0.1/pricedoc/assets/img/products/p1.jpg" string(1) "6" object(MongoDB\Model\BSONArray)#79 (1) { ["storage":"ArrayObject":private]=> array(2) { [0]=> object(MongoDB\Model\BSONDocument)#7 (1) { ["storage":"ArrayObject":private]=> array(1) { ["Comment"]=> object(MongoDB\Model\BSONDocument)#39 (1) { ["storage":"ArrayObject":private]=> array(4) { ["username"]=> string(4) "Alex" ["date"]=> object(MongoDB\BSON\UTCDateTime)#68 (1) { ["milliseconds"]=> string(13) "1611028089000" } ["text"]=> string(21) "1st comment from user" ["rating"]=> string(1) "4" } } } } [1]=> object(MongoDB\Model\BSONDocument)#78 (1) { ["storage":"ArrayObject":private]=> array(1) { ["Comment"]=> object(MongoDB\Model\BSONDocument)#77 (1) { ["storage":"ArrayObject":private]=> array(4) { ["username"]=> string(4) "Alex" ["date"]=> object(MongoDB\BSON\UTCDateTime)#76 (1) { ["milliseconds"]=> string(13) "1611030745000" } ["text"]=> string(8) "good!!!!" ["rating"]=> string(1) "5" } } } } } }  
https://stackoverflow.com/questions/66053240/search-with-php-inside-mongodb February 05, 2021 at 04:20AM

没有评论:

发表评论