php - Mongodb, using aggregate combined with filter? -
i'm trying convert php script based in mysql database run on mongodb database. have resolved major queries except one.
imagine have library (this collection), every document book entry. need know how many distinct authors in library wroten in language (another field).
at moment have code , don't know how continue:
$test = array( array( '$group' => array( '_id' => array('author' => '$author' ) ) ) ); $out = $db->$collection->aggregate($test);
thanks.
if want set filter, need start pipeline $match,the command be:
db.collection.aggregate([ { "$match" : {lang : "en"} }, {"$group":{"_id": {"author" : "$author"} , "total" : {"$sum" : 1} }} ])
if convert php be:
$test = array( array('$match' => array("lang" => "en")), array( '$group' => array( "_id" => array('author' => '$author'), "total" => array('$sum' => 1) ), ), );
in case have pass language in first step (match/filter).
if want "all languages" need set language in $group operator
db.collection.aggregate([ {"$group":{"_id": {"author" : "$author", lang : "$lang"} , "total" : {"$sum" : 1} }} ])
i allow force in php
you can find more informations $match here: http://docs.mongodb.org/manual/reference/operator/aggregation/match/
php mongodb aggregate
No comments:
Post a Comment