How to use $inc in ODM

How do i use $inc using the object document mapper.

db.products.update(
   { sku: "abc123" },
   { $inc: { quantity: -2, "metrics.orders": 1 } }
)
edited May '16

Have you tried with something like

db.products.update(
   [ sku => "abc123" ],
   [ "$inc" => [ quantity => -2, "metrics.orders" => 1 ] ]
)

or using array() instead of [] if using older PHP?



13.0k

hmm, i am using the object document mapper. when i use ::update it says, function cannot be found



13.0k

In the end i stick to the below. Any difference from using $inc? performance wise?

public function _incrementPageviews($id){
    $page = Page::findById( $id );
    $page->pageViews += 1;
    $page->save();
}