0

so my mongodb looks like this:

{
        "_id" : ObjectId("60004358bd500000d3006cb3"),
        "Name" : "Tsakiris",
        "VAT" : "099360123",
        "DOY" : "----",
        "Address" : "some street",
        "TEL" : "2109750115",
        "Products" : [
                {
                        "pID" : "099360123/1",
                        "pNAME" : "Nike Air Force",
                        "pPRICE" : "95",
                        "pBRAND" : "Nike",
                        "pDESCRIPTION" : "50% green eco frindly",
                        "pCATEGORY" : "Trainers",
                        "pDEPARTMENT" : "Men/Shoes/Trainers",
                        "pTHUMBNAIL" : "http://127.0.0.1/pricedoc/assets/img/products/p1.jpg"
                },
                {
                        "pID" : "099360123/2",
                        "pNAME" : "Renato Garini boots",
                        "pPRICE" : "150",
                        "pBRAND" : "Renato Garini",
                        "pDESCRIPTION" : "autumn collection boot 2020",
                        "pCATEGORY" : "Βoot",
                        "pDEPARTMENT" : "Men/Shoes/Βoot",
                        "pTHUMBNAIL" : "http://127.0.0.1/pricedoc/assets/img/products/p2.jpg"
                }
        ],
        "nextProductCounter" : 3
}

I'm trying to select data with php

$options = ["typeMap" => ['root' => 'array', 'document' => 'array']];
$m = new MongoDB\Client("mongodb://127.0.0.1/", [], $options);
//$m= new MongoDB\Client ("mongodb://127.0.0.1/");
$db = $m->stores;
$collection = $db->storeinfo;   
$pID = $VAT."/1";
$products = $collection->findOne(array("Products.pID" => $pID));
//var_dump($products);
foreach ($products as $product) {
   echo $product;
}

But i have the Notice: Array to string conversion in D:\xampp\htdocs\pricedoc\assets\includes\library.php

So i tried to convert it in json and search at json string but still nothing.

I tried many things that found on the internet but it seems that i have stuck. Is there a way to have the product's variables ?

example:

echo $product[Product.pID];     //output => 099360123/1

echo $product[Product.pName];   //output => Nike Air Force
1
  • Uncomment var_dump($products);. What does that show? Commented Jan 14, 2021 at 16:14

1 Answer 1

1

I don't know MongoDB or what it returns, but you probably need to loop Products. Given that JSON, they will be objects:

foreach ($products->Products as $product) {    
    echo $product->pID;     //output => 099360123/1        
    echo $product->pName;   //output => Nike Air Force    
}

Or if arrays are returned:

foreach ($products['Products'] as $product) {    
    echo $product['pID'];     //output => 099360123/1        
    echo $product['pName'];   //output => Nike Air Force    
}

Or some combination or the two. Your var_dump($products); will show you if you uncomment it.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.