Group by in doctrine mongodb - mongodb

I'm trying to make a group by in mongodb with doctrine.
I have made sentence with createQuery (doctrine) but not with MongoDB.
$qb = $this->_em->createQuery("Select sum(p.cantidad) FROM Application\Entity\ConjuntoProductos p where p.cesta=:cesta group by p.cesta");
$qb->setParameter('cesta', $idCesta);
$results=$qb->getResult();
I need to do in MongoDB.
Thank You.

The solution:
$results = $qb
->group ( array (
'id' => 1
), array (
'total' => 0
) )->reduce ( 'function ( curr, result ) { result.total += curr.cantidad;}' )
->field ( 'cesta.id' )->equals ( $idCesta )
->getQuery ()->execute ();

Related

How to update the table using array of values in Laravel 8

I do have a two array's, I would like to update the Db based on that.
Array ( [0] => Array ( [name] => 1 ) [1] => Array ( [name] => 1 ) )//Id array
Array ( [0] => Array ( [quantity] => 740 ) [1] => Array ( [quantity] => 705 ) ) //The values that I need to update based on the previous array.
Some thing like this,
DB::table('stock')->where('id' , $id_array)->update($value_array);
How Can I achieve that?
$id_array = array(
['name' => 1],
['name' => 2]
);
$value_array = array(
['quantity' => 740],
['quantity' => 705]
);
foreach($id_array as $key => $value) {
DB::table('stock')->where('id' , $id_array[$key]['name'])->update( $value_array[$key]['quantity']);
}
Assuming both arrays are equal size, we can loop through the $id_array and use the key index value for the $value_array.

Creating an index on a nested field

I have a MongoDB document that is structured similar to the structure below follows. There are is a large number of these documents with thousands of people in them, so I want to speed things up by putting indexes on the people.#.search_columns.surname and people.#.search_columns.givenname. Howe can I do this in MongoDB? Thanks for your help.
[_id] => MongoId Object (
[$id] => 53b1b1ab72f4f852140dbdc9
)
[name] => People From 1921
[people] => Array (
[0] => Array (
[name] => Barada, Valentine
[search_columns] => Array (
[surname] => Array (
[0] => Mardan,
[1] => Barada
)
[givenname] => Array (
[0] => Valentine
)
)
)
[1] => Array (
[name] => Barsaloux, Nicholas
[search_columns] => Array (
[surname] => Array (
[1] => Barsaloux
)
[givenname] => Array (
[0] => Nicholas
)
[place] => Array (
)
)
)
)
You can create indexes for nested fields using the dot notation:
db.collection.ensureIndex({'people.search_columns.surname': 1});
db.collection.ensureIndex({'people.search_columns.givenname': 1});

How to increase MongoDB performance

I am developing an application in MongoDB, CakePHP. I have 145,000 record in my database. When I query for records using following command then it takes 12seconds. Which is very bad for my application.
$params= array('aggregate'=>array(
array('$project'=>array('as'=>1,'pid'=>1,'st'=>1)),
array('$unwind'=>'$as'),
array('$match' => array('pd'=>array('$gt'=>$f,'$lt'=>$t),'pid'=>$project_id)),
array('$group'=>array('_id'=>'$as')),
array('$sort'=>array('_id'=>1)),
array('$limit'=>10)
)
);
$results = $this->Detail->find('all',array('conditions'=>$params));
Can anyone help me in reducing time for query.
I have indexes on as & pid. My system RAM is 1.5GB.
I got following data as result
[1] => Array
(
[Detail] => Array
(
[_id] => Array
(
[0] => "superfone" Llc,moscow,ru
)
)
)
[2] => Array
(
[Detail] => Array
(
[_id] => Array
(
[0] => "superphone" Llc,moscow,ru
)
)
)
Before performing the find, make sure to set the recursive level to the level you need, then report back on your performance.
$this->Detail->recursive = -1; // no joins
results = $this->Detail->find('all',array('conditions'=>$params));
or
$this->Detail->recursive = 0; // data + domain
results = $this->Detail->find('all',array('conditions'=>$params));
Not sure what level of recursive you need, so give them a test, but default is 1 and it can be a costly exercise.
ref
http://book.cakephp.org/2.0/en/models/model-attributes.html#recursive
The reference also says to make -1 your default, and raise the recursive levels as you need them, this can be done by adding the following to AppModel
public $recursive = -1;

Getting last row in mongodb

I am using find() function in mongodb and got a record in following format
Array
(
[_id] => MongoId Object
(
[$id] => 52a561ea78e9288b568b4567
)
[friendID] => 1
[name] => Shobhit Srivastav
[senderID] => 2
[receiverID] => 1386570218
[receiverType] => TW
[receiverUserID] => 3
[status] => 0
)
Array
(
[_id] => MongoId Object
(
[$id] => 52a5623178e928d8568b4567
)
[friendID] => 2
[name] => Sachin Tendulkar
[senderID] => 2
[receiverID] => 1386570289
[receiverType] => TW
[receiverUserID] => 3
[status] => 0
)
but I want record of last row which are inserted in the table. how can I find??
Thanks in advance!!
If you want to get last record inserted in the table, then sort by ObjectId in descending order:
sorting on an _id field that stores ObjectId values is roughly
equivalent to sorting by creation time.
and get first record:
db.collection.find().sort( { _id : -1 } ).limit(1);
With php driver it will look like
$doc = $collection->find()->sort(array("_id" => -1))->limit(1)->getNext();

UNION and NOT IN Mysql Operation with Zend framework

I need to execute the following mysql query in Zend;I am not an excpert with Zend framework
SELECT `optionride`.`featureoption_id`,
`optionride`.`featureoptionride_id`,`foption`.`featureoptionblock_id`,
`foption`.`labelname`,`optionride`.`value` FROM
`engine4_ride_featureoptionrides` AS `optionride`
LEFT JOIN `engine4_ride_featureoptions` AS `foption`
ON foption.featureoption_id = optionride.featureoption_id
WHERE (optionride.ride_id = '1' ) AND (foption.featureoptiontab_id= '2' )
UNION
SELECT `foption`.`featureoption_id`, null as
`featureoptionride_id`,`foption`.`featureoptionblock_id`,
`foption`.`labelname`,null as `value` FROM `engine4_ride_featureoptions` AS `foption`
WHERE (foption.featureoptiontab_id= '2' ) AND `foption`.`featureoption_id` NOT IN
(
SELECT `optionride`.`featureoptionride_id` FROM `engine4_ride_featureoptionrides`
AS `optionride`
LEFT JOIN `engine4_ride_featureoptions` AS `foption` ON
foption.featureoption_id = optionride.featureoption_id
WHERE (optionride.ride_id = '1' ) AND (foption.featureoptiontab_id= '2' )
)
Anybody can help me please.
You can put all your query asis in the $db->fetch().
Also you can use $db->select()->union(array($sql1, $sql2)), where $sql1, $sql2 can be $db->select() or a string ("select...").
For the NOT IN you can use $db->where('someField NOT IN (?)', array());