Skip to content

Instantly share code, notes, and snippets.

@tvdsluijs
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tvdsluijs/4dfb175a33b7913a750c to your computer and use it in GitHub Desktop.
Save tvdsluijs/4dfb175a33b7913a750c to your computer and use it in GitHub Desktop.
Mongodb php aggregation $match usage
$m = new MongoClient(); // connect to local mongodb
$db = $m->products; //select 'mongo database' named products
$clothing_col = $db->clothing; //create a collection from the 'table' clothing
//create the aggregation
//create the Match on clothing-category
$ops = array(
array(
'$match' => array("category" => 'socks')
)
);
$g = $clothing_col->aggregate($ops); //put the aggregation in the function
$products = $g['result']; //put results in your own var
foreach($products as $product){
echo $product['product_name'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment