87276

MongoDB in PHP using aggregate to group by _id is null not working

I'm trying to use this MongoDB command and filter, its dosen work when i translate it to PHP, hope there are one there can help me to explain why its not working?

<strong>MongoDB syntax:</strong>

db.getCollection('product').aggregate( [ { $match: { 'Store.Title': { $regex: 'apc', $options: 'gi' } } }, { $group: { _id: null, count: { $sum: 1 } } } ] );

<strong>Output from MongoDB:</strong>

{ "result" : [ { "_id" : null, "count" : 774.0000000000000000 } ], "ok" : 1.0000000000000000 }

<strong>PHP Filter array:</strong>

[ [ '$match' => [ 'Store.Title' => ['$regex' => 'apc', '$options' => 'gi'] ] ],[ '$group' => [ '_id' => 'null', 'count' => [ '$sum' => 1 ] ] ] ]

<strong>Output from PHP:</strong>

Array ( [result] => Array ( ) [ok] => 1 )

i hope there are simple explaining on this issue thanks for helping guys.

Answer1:

Use the <strong>MongoRegex</strong> object:

$search = "apc"; $where = array('Store.Title' => array('$regex' => new MongoRegex("/^$search/gi"))); // $where = array('Store.Title' => array('$regex' => new MongoRegex("/".$search."/gi"))); [ [ '$match' => $where ], [ '$group' => [ '_id' => 'null', 'count' => [ '$sum' => 1 ] ] ] ]

Recommend

  • Python doesn't have opcode cacher?
  • Activate APC at XAMPP ( Windows )
  • mod_fcgid with php opcode caching
  • How to retrieve / find all elements of a nested array in MongoDB Java
  • Run JMX monitor collector listener using Jmeter Maven Plugin
  • magento visitor logs
  • How to rewrite Collection?
  • How to remove a product from a category magento 1.5
  • How to get list of users who's birthday is today in MongoDB
  • Sending Content-Type application/x-www-form-urlencoded WSO2 ESB
  • Scala using regex with or syntax in match case statement
  • Negating Regex PO BOX
  • Efficient User-Agent Regex to find Safari in Python
  • Counting problem C#
  • Xaml, wpf image position and crop issue
  • Magento get URL before current
  • Open Existing DB in MySQL WorkBench
  • Get specific string
  • Redux Form - Not able to type anything in input
  • How can I extract results of aggregate queries in slick?
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • OpenGL 3.3 on Mac OSX El Capitan with LWJGL
  • Extracting HTML between tags
  • How do I change content of ComboFieldEditor?
  • Display issues when we change from one jquery mobile page to another in firefox
  • Regex thinks I'm nesting, but I'm not
  • Deleting and Updating values from a cusrsor adapter
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Modifying destination and filename of gulp-svg-sprite
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • Weird JavaScript statement, what does it mean?
  • To display the title for the current loaction in map in iphone
  • Do I've to free mysql result after storing it?
  • XCode can't find symbols for a specific iOS library/framework project
  • SQL merge duplicate rows and join values that are different
  • trying to dynamically update Highchart column chart but series undefined
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Getting Messege Twice Using IMvxMessenger
  • Turn off referential integrity in Derby? is it possible?