MongoDB & PHP only gets the product that matches the barcode

I have this JSON and you can see under the products, I have barcodes for each product, what I want to do is get only the information corresponding to the product barcode

{
  "company": "village",
  "logo": "http:\/\/www.incard.com.au\/newsite\/template\/images\/movieticket\/4cinemas\/villagetop.png",
  "products": [
    {
      "barcode": "236690091",
      "name": "Weekday",
      "logo-URL": "http: \/\/www.incard.com.au\/newsite\/template\/images\/movieticket\/4cinemas\/ticketpic1.png",
      "price": "12.50",
      "discount": "1.50"
    },
    {
      "barcode": "236690092",
      "name": "Weekend",
      "logo-URL": "http: \/\/www.incard.com.au\/newsite\/template\/images\/movieticket\/4cinemas\/ticketpic1.png",
      "price": "13.50",
      "discount": "1.60"
    },
    {
      "barcode": "236690093",
      "name": "Gold Class",
      "logo-URL": "http: \/\/www.incard.com.au\/newsite\/template\/images\/movieticket\/4cinemas\/ticketpic1.png",
      "price": "13.50",
      "discount": "1.60"
    }
  ],
  "store_name": "movies"
}

      

for example if i hit 236690091 i only what the database (MongoDB) returns

"barcode": "236690091",
      "name": "Weekday",
      "logo-URL": "http: \/\/www.incard.com.au\/newsite\/template\/images\/movieticket\/4cinemas\/ticketpic1.png",
      "price": "12.50",
      "discount": "1.50"

      

not every product.

Here is what I have tried

public function getbarcode($barcode)
    {
        // select a collection (analogous to a relational database table)
         $collection = $this->db->movies->products;

        // find everything in the collection
        $cursor = $collection->find(array("barcode" =>"{$barcode}"));

        $test = array();
            // iterate through the results
            while( $cursor->hasNext() ) {
                $test[] = ($cursor->getNext());
            }
        //Print Results 
        print json_encode($test);

    }

      

+3


source to share


2 answers


You cannot do this. MongoDB will always return the complete document and will not let you return only the nested part you want to search for. I would suggest splitting the products into your collection and then adding company information to each product. It also costs 16MB of document if you have many products for each company.

Without changing your schema, the following code should work:



public function getbarcode($barcode)
{
    $products = array();
    $collection = $this->db->movies->products;

    foreach( $collection->find( array( 'products.barcode' => $barcode ) ) as $item )
    {
        foreach( $item->products as $product )
        {
            if ( $product['barcode'] == $barcode )
            {
                $products[] = $item;
            }
        }
    }
    return $products;
}

      

+1


source


You cannot do it the way you want. MongoDB will return only whole documents or some fields (if you specify them in the query) from documents. You cannot only return values ​​that match your query.

You can create a separate collection that only stores product objects (with a link to the collection that stores company data) where you can directly query the product data you want.

If you can't / won't create another collection, you can find all documents that have a product with the specified barcode and filter them using PHP.

For this second approach, your request should be:



$collection->find(array("products.barcode" =>"{$barcode}"), 
                  array('products' => 1));

      

With this query, you hit objects and only return documents that have the barcode you are looking for.

Also, in this query, you will return a property products

from your document, not the whole document. The property products

will contain all child objects, not just the one you are trying to find.

In your loop, while

you should check the values ​​and filter them correctly.

0


source







All Articles