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.
Related
It's my first contact to mongodb, I'm coding PHP7 with MONGODB database and don't know hown to query.
My query is working on mongo:
db.points.find(
{ coordenadas:
{ $geoWithin:
{ $centerSphere:
[ [ -23.010382,-43.476006 ] , 10 / 3963.2 ]
}
}
}
)
But I don't know how to coding it on PHP:
...
try {
$mongo = new MongoDB\Driver\Manager('mongodb://localhost:27017');
$filter = [
THE QUESTION IS HERE!
HOW TO CODING THE MONGO'S FIND
];
$query = new MongoDB\Driver\Query($filter);
$rows = $mongo->executeQuery(“points”, $query);
...
PHP code for the above query will be something like this :
$condition = array(
'coordenadas' => array(
'$geoWithin' => array(
'$centerSphere' =>
array(array( -23.010382,-43.476006 ), 10 / 3963.2)
)
)
);
If your db name is "my_db" and collection name "point_collection"
$collection = $mongo->my_db->point_collection;
$result = $collection->find($query);
You can refer the documentation for find();
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]);
I have a find method that uses a DISTINCT clause to get results from my Model. The Controller code looks like below
$options = array(
'limit' => 10,
'fields' => array(
'DISTINCT id', 'title',
),
'contain' => array(
'Dealer' => array('id'),
),
'paramType' => 'querystring'
);
$this->Paginator->settings = $options;
$cars = $this->Paginator->paginate('Car'); // returns 6 distinct rows
The above query return 6 distinct rows and 12 total rows. So when I am displaying, the screen shows 6 distinct rows
However in the View, when I use
echo $this->Paginator->param('count'); // returns 12
I get a count of 12
I checked the SQL Log and noticed that the count query is not using the distinct clause. Any idea how I can override the Paginator count query to use the DISTINCT clause?
Found the solution,
In controller add distinct as an array parameter with other pagination options. So if I was trying to retrieve a list of Cars in my inventory with 10 cars at a time, the options would have a DISTINCT clause in the fields parameter and a separate parameter called distinct would also be added as shown below
$options = array(
'conditions' => $conditions,
'joins' => $joins,
'limit' => 10,
'fields' => array(
'DISTINCT Car.id', 'title', 'user_id'),
'contain' => array(
'Dealer' => array('id'),
),
'paramType' => 'querystring',
'distinct' => 'Car.id'
);
$this->Paginator->settings = $options;
$cars = $this->Paginator->paginate('Car');
In Model, use the below function to override the original paginateCount method
public function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
$parameters = compact('conditions', 'recursive');
if (isset($extra['distinct'])) {
$parameters['fields'] = 'DISTINCT ' . $extra['distinct'];
$count = $this->find('count', array_merge($parameters, $extra));
} else {
// regular pagination
$count = $this->find('count', array_merge($parameters, $extra));
}
return $count;
}
No change in View
I'm using mongodb 2.4 and added fulltext index to the "title" field in one the collection. How should I search something in that field using php?
This is the code I use right now:
$params = array(
'_id' => array(
'$gt' => (int)$gt
)
);
$r = $this->collection->find( $params )->limit($limit);
This seem to be the answer to my question:
<?php
$result = $db->command(
array(
'text' => 'bar', //this is the name of the collection where we are searching
'search' => 'hotel', //the string to search
'limit' => 5, //the number of results, by default is 1000
'project' => Array( //the fields to retrieve from db
'title' => 1
)
)
);
http://www.php.net/manual/en/mongodb.command.php#111891
I have two tables, User and Company. I have joining them like this:
$table = $this->getDbTable();
$select = $table->select();
$select->setIntegrityCheck( false );
$select->from( array('User'), array( 'id' => 'id',
'name' => 'User.name',
'gender' => 'User.gender',
'Company_id' => 'User.Company_id'
));
$select->join( 'Company', 'Company.id = User.Company_id',
array( 'Company_name' => 'Company.name' ,
'Company_address' => 'Company.address'
));
$rows = $table->fetchAll( $select );
It is working and giving me accurate result. Problem is that I have to mentions column names in above codes. I want to get all columns without mentioning them in above piece of code.
For example I want something like this that get all columns(But it is not providing all column values):
$table = $this->getDbTable();
$select = $table->select();
$select->setIntegrityCheck( false );
$select->from( array('User') );
$select->join( 'Company', 'Company.id = User.Company_id' );
$rows = $table->fetchAll( $select );
Thanks
Leaving away the second parameter to the from call should work: http://framework.zend.com/manual/en/zend.db.select.html#zend.db.select.building.columns