PHP Classes

File: doc/5-other-operations.md

Recommend this page to a friend!
  Classes of Xavier Pérez   XMongoDB   doc/5-other-operations.md   Download  
File: doc/5-other-operations.md
Role: Documentation
Content type: text/markdown
Description: Manual
Class: XMongoDB
Build and execute queries to a MongoDB database
Author: By
Last change: Query aggregations
Aggregations
Aggregate
Aggregations
Date: 8 years ago
Size: 3,256 bytes
 

Contents

Class file image Download

Other Operations

Get last inserted ID

$xmongodb->insert_id();

Add values to an existing set

$xmongodb->addToSet('myfield1',array('myvalue1','myvalue2');

References: http://docs.mongodb.org/manual/reference/operator/update/addToSet/

Increment the value of a field

Increment only one field

$xmongodb->inc('myfield1',1)
    ->where(array('myfield2' => 'myvalue2'))
    ->update('collection');

Increment multiple fields

$xmongodb->inc(array('myfield1','myfield2),1)
    ->where(array('myfield3' => 'myvalue3'))
    ->update('collection');

References: http://docs.mongodb.org/manual/reference/operator/update/dec/

Decrement the value of a field

Decrement only one field

$xmongodb->dec('myfield1',1)
    ->where(array('myfield2' => 'myvalue2'))
    ->update('collection');

Decrement multiple fields

$xmongodb->dec(array('myfield1','myfield2),1)
    ->where(array('myfield3' => 'myvalue3'))
    ->update('collection');

Now, MongoDB uses $inc function, you must to specify positive values in the $xmongodb->dec, this function converts to negative.

References: http://docs.mongodb.org/manual/reference/operator/update/inc/

Pop an element of an array

Removes the first element in an array

$xmongodb->pop('myarray1')
    ->where(array('myfield3' => 'myvalue3'))
    ->update('collection');

Removes the first element in all elements specified

$xmongodb->pop(array('myarray1','myarray2')
    ->where(array('myfield3' => 'myvalue3'))
    ->update('collection');

References: http://docs.mongodb.org/manual/reference/operator/update/pop/

Pull an element of an array

Removes the element of an array that match the value

$xmongodb->pull('myfield1', array('postalcode'=>'08080'))
    ->update('collection');

References: http://docs.mongodb.org/manual/reference/operator/update/pull/

PullAll elements of an array

The pullAll operator removes all instances of the specified values from an existing array. Unlike the pull operator that removes elements by specifying a query, pullAll removes elements that match the listed values.

$xmongodb->pull('myfield1', array('country'=>34, 'city' => 'Barcelona'))
    ->update('collection');

References: http://docs.mongodb.org/manual/reference/operator/update/pullAll/

Aggregations

Aggregations must to be done in array groups, one for each group (sort, project, group)

        // Group by Country, order by Country
        $aggregate = array();
        $aggregate[] = 
            array(
                '$sort' => array('name',1)
        );
        $aggregate[] = 
            array(
                '$group' => array(
                    '_id' => array(
                        'Country' => '$country'
                        ),
                    'firstPerson'=> array ('$first','$name'},
                    'lastPerson' => array ('$last','$name'},
                    'count' => array('$sum',1)
                ),
        );
        $query = $xmongodb->aggregate('collection',$aggregate);
        $result = $query['result'];

This will find a number of persons found by country, showing the first and the last name.