Doing advanced MongoDB updates in Lithium

I wanted to change a nested array value without first loading an entire MongoDB document in Lithium, but the documentation wasn’t clear. One of the developers on the #li3 IRC channel pointed me in the right direction.

Since the “atomic” option is defaulted to true, the $set is implied. The following example is with a model called “Lesson”:

$conditions = array('_id' =>  $_id); //conditions for document(s) to change
$data = array(('page_list.123.title') => $title); //nested value I want to change
Lesson::update($data,$conditions);

I hope that points you in the right direction. It’s like the native MongoCollection::update but with the first two parameters switched. You can also include a third parameter for options. Soon the update method will be patched to allow for MongoDB upserts.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s