Skip to content

Instantly share code, notes, and snippets.

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 talcual/0bf4d4ce2f35f3a6feeca0a5c325a160 to your computer and use it in GitHub Desktop.
Save talcual/0bf4d4ce2f35f3a6feeca0a5c325a160 to your computer and use it in GitHub Desktop.
Mongodb php aggregation $match $or 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 = shoes or brand = nike
$ops = array(
array(
'$match' =>
array('$or' =>
array(array("category" => 'shoes'),
array("brand" => 'nike')))
)
);
$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