Mongo mapper use aggregation framework - php

Can someone please point out my error, trying to sum a field(amount) using userId field?
https://fatfreeframework.com/3.6/mongo-mapper
Here is my code, this is returning me all documents matched with userId but not the sum.
$f3 = \Base::instance();
$mapper = new \DB\Mongo\Mapper($f3->get('MongoDB'),'transactions');
$filter = array('userId'=>'452');
$options = array(
array(
'group' => array(
'_id' => array('userId' => $userId),
'amount' => array('$sum' => 'amount')
)
)
);
$data = $mapper->find($filter, $options);
echo "<pre>";
print_r($data);
exit;

The mongo mapper uses the db.collection.group() method rather than the aggregation framework. Therefore you won't be able to call accumulators such as $sum.
Instead you must use the following syntax:
$group = [
'keys'=>['userId'=>1],
'initial'=>['sum'=>0],
'reduce'=>'function(obj,result){result.sum+=obj.amount;}',
'finalize'=>'function(result){}',
);
$data = $mapper->find($filter, ['group'=>$group]);

Related

Add parameter to function php in option query mongo?

My mongoDB have field: $creatime, I want add field to function php.
How do it?
$ops = array(
array('$match' => $query),
array('$addFields' => array('date_code' => functionPHP('$create_time'))),
);
$results = $col->aggregate($ops,array('cursor' => array('batchSize' => 200)));

$limit in mongodb aggregation as an optional parameter?

I have an api with one optional parameter called limit, which takes an integer and limits the number of documents returned from the api in a get request.
Implementing this limit is fine in my PHP application when it is a required parameter, but when its not specified as part of my get request, what is the best way to handle it?
is there for example, a way to define $limit and set it to all documents? (in pseudo code, $limit = none)
$list = $collection->aggregate( array( array('$match' => array( ... )),
'$project' => array ( ... )), '$limit' => intval($this->limit) ));
$this->limit //this is the optional input parameter
As you can see above, when the limit parameter is required it functions as required. Now when its ommitted, how can I keep the above code but specify no limit?
You could manually generate your where clause.
// Declare a where clause with your fixed conditions
$whereClause = array(array('$match' => array(...)), array('$project' => array(...)));
// Check if there's a limit
if($this->limit != 0)
array_push($whereClause, array('$limit' => $this->limit));
// Finally, call with the where clause we've generated above
$list = $collection->aggregate($whereClause);
Short answer No!
But, you can use an if/else condition like this:
<?php
if(intval($this->limit) != 0 ) {
$list = $collection->aggregate( array(
array('$match' => array( ... )),
array('$project' => array ( ... ))
array('$limit' => intval($this->limit))
);
));
} else {
$list = $collection->aggregate( array(
array('$match' => array( ... )),
array('$project' => array ( ... )),
);
}
?>

Get distinct values and case insensitive with MongoDb

Is it possible to get distinct values and be case insensitive? I found a couple people saying to use the aggregate function but I would keep getting errors like: exception: field path references must be prefixed with a '$' ('Classification'.
public function getDistinctValues($cid, $table, $column)
{
$mongo = new MongoClient();
$db = $mongo->leadworks;
$collection = new MongoCollection($db, $table);
//$result = $collection->distinct($column);
$result = $collection->aggregate(array(
array(
'$unwind' => $column,
),
array(
'$group' => array(
'_id' => array(
'$toLower' => array(
$column,
),
),
),
),
));
return $result;
}
This is how my document looks:
{
_id:2065565,
Date:new Date(1411732940000),
Email:"email#email.com",
Other - Industry:null,
Other - Job Duties:null,
First Name:"Test Name",
Last Name:"Test Last Name",
Title:"engineer",
Company:"Company Name Here",
Country:"UNITED STATES",
State:"MI",
Zipcode:"48071-1514",
Phone number:"777-777-7777",
Company type:"Systems integration",
}
I'm specifically trying to get distinct values for Company and ignore case.

Salesforce PHP SOAP API. How to populate a lookup field?

I have a custom object called "Chapter_Detail__c". I am creating a LEAD, and a lead has a look up field(link_id__c) which is mapped with "Chapter_Detail__c".
How can i populate this look up field while creating the Lead.
$sObject = new SObject();
$fieldsToInsert = array (
'FirstName' => $name_rsform,
'LastName' => $username_rsform,
'Company' => 'TestSession',
'Full_Name__c' => $name_rsform,
'Chapter_Name__c' => $chapter_name_rsform,
'Role__c' => $role_rsform_label,
'Email__c' => $email_rsform,
'Branch_of_service__c' => $bos_f,
// 'link_id__c' => $chapter_id_rsform, - DOES NOT WORK THIS WAY ITS A LOOKUP RELATIONSHIP WITH OBJECT "Chapter_Detail__c"
);
$sObject->fields = $fieldsToInsert;
$sObject->type = 'Lead';
//echo "**** Creating the following:\r\n";
$createResponse = $mySforceConnection->create(array($sObject));
//print_r($createResponse);
echo 'Get ID after create';
$ids = array();
foreach ($createResponse as $createResult) {
print_r($createResult);
array_push($ids, $createResult->id);
}
$idRecieved = $ids[0];
Any kind of help will be appreciated. Thanks.

Find Records in Mongo DB with PHP where ID != X

is it possible to find all records in an collection where the MongoID is not in
an provided array?
Something like this (?):
$search = array(
'_id' => array('$ne' => $ids)
'readby' => array('$ne' => $userId) // works
);
Iam using PHP with the Mongo Extension.
Use $nin instead of $ne with arrays. Something like:
$search = array(
'_id' => array('$nin' => $ids),
'readby' => array('$ne' => $userId)
);
should do what you want.

Categories